Skip to content

Instantly share code, notes, and snippets.

@smallnewer
Created September 16, 2013 04:28
Show Gist options
  • Save smallnewer/6576699 to your computer and use it in GitHub Desktop.
Save smallnewer/6576699 to your computer and use it in GitHub Desktop.
判断浏览器是否存在该bug:IE中setAttribute设置className的bug.标准浏览器需要用'class'而IE需要用'className'
/*****
* 在标准浏览器中,设置标签的class可以使用两种方法:
* div.className='test';和div.setAttribute("class","test");
* 得到:
* <div class='test'></div>
* 但是大概由于class是关键字的缘由,在IE中处理策略不同:
* 需要用className来设置
* div.setAttribute("className","test");
* 得到:
* <div class='test'></div>
* div.className === "test"; // true
* 而在标准浏览器中得到:
* <div classname='test'></div>
* div.className === 'test'; //false,undefined
* 所以在不同浏览器中,使用setAttribute设置class要处理这个问题
* 判断是否有这个问题的方法很简单:
* * *
*/
function canSetClassNameByAttr(){
var div = document.createElement("div");
div.setAttribute("className", 't');
return div.className === 't';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment