首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

页面操作的简单化

2012-09-08 
页面操作的简化$ : function(id, scope) {??if (arguments.length 1 || !scope) {???return document.g

页面操作的简化

$ : function(id, scope) {
??if (arguments.length == 1 || !scope) {
???return document.getElementById(id);
??}
??var childNodes = scope.childNodes;
??var len = childNodes.length;
??for (var i = 0; i < len; i++) {
???if (childNodes[i].nodeType == 1) {
????if (childNodes[i].id == id) {
?????return childNodes[i];
????}
????else {
?????var returnNode = arguments.callee.apply(null, [id, childNodes[i]]);
?????if (returnNode) {
??????return returnNode;
?????}
????}
???}
??}
??return null;
?},
?$attr:function(dom,attrName){
??try{
???return dom[attrName]!=null?dom[attrName]:dom.getAttribute(attrName);
??}catch(e){
?? ?return null;
??}
?},
?$c : function(tagName) {
??return document.createElement(tagName);
?},
?$a : function(parentNode, childNode) {
??parentNode.appendChild(childNode);
?},
?$rs : function(selectElem, data) {
??if(!(selectElem&&data))
???return;
??var options = selectElem.options;
??var len = options.length;
??while (len--) {
???options.remove(len);
??}
??if (data && data.length != 0) {
???var l = data.length;
???for (var index = 0; index < l; index++) {
????var op = document.createElement("option");
????op.value = data[index].ids || data[index].id;
????op.text = data[index].name;
????options.add(op);
???}
???var firstValue = selectElem.options[0].value;
???if (firstValue && firstValue != "0")
????selectElem.value = selectElem.options[0].value;
???else if (data.length > 1) selectElem.value = selectElem.options[1].value;
??}
?},
?isDefined : function(obj) {
??return typeof obj != 'undefined' && !BusCard.util.isNull(obj);

?},
?isObject : function(obj) {
??return typeof obj == 'object' && !BusCard.util.isNull(obj);
?},
?isArray : function(obj) {

??return obj && obj.constructor == Array;
?},
?isNull : function(obj) {
??return obj === null;
?},
?isFunction : function(obj) {
??return typeof obj == 'function';
?},

?isString : function(obj) {
??return typeof obj == 'string';
?},
?getXhr : function() {
??var xhr = null;
??try {
???window.XMLHttpRequest ? xhr = new XMLHttpRequest() : xhr = new ActiveXObject("Microsoft.XMLHTTP");
??}
??catch (e) {}
? return xhr;

?},

?parse : function(jsonData) {
??return (new Function("return " + jsonData)).apply(null, []);
?},

?

?

//函数中声明的所有变量在整个函数中都有定义。

例:

var scope = "global";

function f(){

??? alert(scope);???? //display "undefined"

??? var scope = "local";

??? alert(scope);?? //display "local"

}

f();

热点排行