Created
January 4, 2013 13:16
-
-
Save anonymous/4452522 to your computer and use it in GitHub Desktop.
常用的JS操作方法
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Array.prototype.slice.call(obj, 0) | |
将一个对象转换为数组 | |
*/ | |
~function() { | |
var domjs = { | |
makeArr: function(elems) { | |
return Array.prototype.slice.call(elems, 0); | |
}, | |
each: function(elem, callback) { | |
if (elem.length !== undefined) { | |
for(var i = 0, len = elem.length; i < len; i++) { | |
if(callback.call(elem[i], i, elem[i]) === false) break; | |
} | |
} else { | |
for(name in elem) { | |
callback.call(elem[name], name, elem[name]); | |
} | |
} | |
}, | |
trim: function(str) { | |
return str.replace(/^(\s+)|(\s+)$/g, ""); | |
}, | |
/* | |
以上方法调用采用domjs.each, domjs.trim方式调用. | |
*/ | |
getId: function(id) { | |
return [document.getElementById(id)]; | |
}, | |
getTag: function(tag) { | |
return domjs.makeArr(document.getElementsByTagName(tag)); | |
}, | |
getClass: function(className, elem) { | |
var elem = elem || document; | |
if(document.getElementsByClassName) { | |
return domjs.makeArr(elem.getElementsByClassName(className)); | |
} else { | |
var rs = []; | |
var regexp = new RegExp('\\s'+ className +'\\s'); | |
domjs.each(elem.getElementsByTagName("*"), function(i) { | |
if(regexp.test(' ' + this.className + ' ')) { | |
rs.push(this); | |
} | |
}) | |
return rs; | |
} | |
}, | |
/* | |
动态添加一张样式表(document.createStyleSheet(),chrome不支持),用选择器添加一条规则,然后拿出符合规则的元素,清空样式表。原因:querySelectorAll仅在ie6/7上不支持。 | |
*/ | |
getQuery: function(selector) { | |
if(document.querySelectorAll) { | |
return this.makeArr(document.querySelectorAll(selector)); | |
} else { | |
var styleSheet = document.createStyleSheet(), | |
rs = []; | |
styleSheet.addRule(selector, "a:b"); | |
domjs.each(document.getElementsByTagName("*"), function(i) { | |
if(this.currentStyle.a === 'b') { | |
rs.push(this); | |
} | |
}) | |
styleSheet.cssText = ''; | |
return rs; | |
} | |
}, | |
attr: function(elem, name, value) { | |
if (!elem) | |
return ; | |
if (value) { //set the attribute | |
domjs.each(elem, function() { | |
this.setAttribute(name, value); | |
}); | |
return elem; | |
} else { //get the attribute | |
return elem[0].getAttribute(name); | |
} | |
}, | |
val: function(elem, value) { | |
if (!elem) | |
return; | |
if (value) { | |
elem[0].value = value; | |
return elem[0]; | |
} else { | |
return elem[0].value; | |
} | |
}, | |
addClass: function(elem, value) { | |
if (!elem) | |
return ; | |
domjs.each(elem, function() { | |
this.className += ' '+ value; | |
}) | |
return elem; | |
}, | |
removeClass: function(elem, value) { | |
if (!elem) | |
return ; | |
var str = ""; | |
if (value) { | |
var that = this; | |
domjs.each(elem, function() { | |
str = this.className.split(" "); | |
for (var i=0, slen=str.length; i < slen; i++) { | |
if (str[i] === value) { | |
str[i] = ""; | |
} | |
} | |
str = str.join(" "); | |
this.className = domjs.trim(str); | |
str = ""; | |
}) | |
} else { | |
this.each(elem, function() { | |
this.className = ""; | |
}) | |
} | |
return elem; | |
}, | |
toggleClass: function(elem, value) { | |
//hasAttribute方法不支持IE7/6 | |
}, | |
//style只支持每个样式单独设置 | |
style: function(elem, type, value) { | |
if (value) { | |
domjs.each(elem, function() { | |
this.style[type] = value; | |
}) | |
return elem; | |
} else { | |
var style; | |
if (document.defaultView) { | |
style = document.defaultView.getComputedStyle(elem[0], null); | |
return style[type]; | |
} else { | |
style = elem[0].currentStyle; | |
return style[type]; | |
} | |
} | |
} | |
}; | |
window.domjs = domjs; | |
}() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
創建的gist,怎麼能歸到自己帳戶下面呢,好像這個成了匿名gist了???