Created
November 14, 2012 08:56
-
-
Save andy12530/4071067 to your computer and use it in GitHub Desktop.
JS库中常用的attr方法
This file contains 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
/* | |
如果传来的参数为html集合,则只取第一个节点进行操作 | |
*/ | |
var attr = function(elem, name, value) { | |
if (!elem) | |
return ; | |
if (elem.length > 1) { | |
elem = elem[0]; | |
} | |
if (value) { //set the attribute | |
elem.setAttribute(name, value); | |
return elem; | |
} else { //get the attribute | |
return elem.getAttribute(name); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment