【问题标题】:IE7: How to set the "class" attribute for a dynamically created element in javascript?IE7:如何在 javascript 中为动态创建的元素设置“类”属性?
【发布时间】:2011-03-23 13:54:25
【问题描述】:
看来是这样的:
var img = document.createElement("img");
img.setAttribute("class", "check");
在 IE7 中不起作用,即该类在 css 中指定的样式未应用。它在 IE8、FF 等中运行良好。有没有办法在 IE7 中设置类或者我必须这样做:
img.style.border = ....
谢谢
【问题讨论】:
标签:
javascript
css
class
internet-explorer-7
【解决方案1】:
相信你需要使用:
img.className = "check";
【解决方案2】:
只是:
img.className = "check";
有时,当您尝试使用“setAttribute()”时,Internet Explorer 会感到非常不安。我没有运行虚拟机,但如果你已经下定决心使用“setAttribute()”,那么你可以尝试:
img.setAttribute("className", "check");
但我不知道这会奏效。