JS_attr函數


/*
* 考慮到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>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM