/*
* 考虑到getAttribute(),在IE6,7下对class有兼容性问题所以使用getAttributeNode();
* removeNamedItem(),在IE6,7下动态添加属性后,表现异常.所以使用removeAttribute();
*/
<div title="hello" id="main" dir="rtl" mytitle="whoat">my!</div>
<script type="text/javascript">
var attr = function(ele,name,value){
if(!ele) return;
if(!!value){
var _att = document.createAttribute(name);
_att.nodeValue = value;
ele.setAttributeNode(_att);
return;
}
if(!!ele.getAttributeNode(name)){
var _val = ele.getAttributeNode(name).nodeValue;
return _val;
}else{
return null;
}
}
var removeAttr = function(ele,name){
if(!ele) return;
switch(name){
case "class":
case "className":
ele.removeAttribute("class");
ele.removeAttribute("className");
break;
default:
ele.removeAttribute(name);
}
}
var main = document.getElementById("main");
attr(main,"class","main");
removeAttr(main,"dir");
</script>