Skip to content

Instantly share code, notes, and snippets.

@andy12530
Created November 14, 2012 08:56
Show Gist options
  • Save andy12530/4071067 to your computer and use it in GitHub Desktop.
Save andy12530/4071067 to your computer and use it in GitHub Desktop.
JS库中常用的attr方法
/*
如果传来的参数为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