简体   繁体   中英

IE7: How to set the “class” attribute for a dynamically created element in javascript?

It appears that this:

var img = document.createElement("img");
img.setAttribute("class", "check");

doesn't work in IE7, that is the styles specified by that class in css are not applied. It works fine in IE8, FF etc. Is there a way of setting the class in IE7 or do I have to do:

img.style.border = .... 

Thanks

我相信您需要使用:

img.className = "check";

Just:

img.className = "check";

Internet Explorer can get really upset when you try to use "setAttribute()" sometimes. I don't have a VM running but if you've got your heart set on using "setAttribute()" then you might try:

img.setAttribute("className", "check");

but I don't know that that will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM