Created
February 13, 2013 05:01
-
-
Save rainyjune/4942413 to your computer and use it in GitHub Desktop.
Get or set an attribute for a DOM element
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
function attr(e, n, v) { | |
var fixAttr = { | |
tabindex: 'tabIndex', | |
readonly: 'readOnly', | |
'for': 'htmlFor', | |
'class': 'className', | |
maxlength: 'maxLength', | |
cellspacing: 'cellSpacing', | |
cellpadding: 'cellPadding', | |
rowspan: 'rowSpan', | |
colspan: 'colSpan', | |
usemap: 'useMap', | |
frameborder: 'frameBorder', | |
contenteditable: 'contentEditable' | |
}; | |
var d = document.createElement("div"); | |
d.setAttribute("class", "yuanJS"); | |
var fullSupported = d.className === "yuanJS"; | |
if(e && n && v) { | |
e.setAttribute(fullSupported ? n : (fixAttr[n] || n), v); | |
} else if (e && n) { | |
return e.getAttribute(fullSupported ? n : (fixAttr[n] || n)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment