使用classList的代码在IE中不起作用? [英] Code with classList does not work in IE?
本文介绍了使用classList的代码在IE中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用以下代码,但在IE中失败了。
消息是:
I'm using the following code, but having it fail in IE. The message is:
无法获取属性'add'的值:object为null或undefined
Unable to get value of the property 'add': object is null or undefined"
我认为这只是一个IE支持问题。你如何让以下代码在IE中运行?
I assume this is just an IE support issue. How would you make the following code work in IE?
任何想法?
var img = new Image();
img.src = '/image/file.png';
img.title = 'this is a title';
img.classList.add("profilePic");
var div = document.createElement("div");
div.classList.add("picWindow");
div.appendChild(img);
content.appendChild(div);
推荐答案
classList
属性。虽然IE10 +支持它。
使用 className + =..
相反。注意:不要省略空格:应在以空格分隔的列表中添加类名。
The classList
property is not supported by IE9 and lower. IE10+ supports it though.
Use className += " .."
instead. Note: Do not omit the space: class names should be added in a white-space separated list.
var img = new Image();
img.src = '/image/file.png';
img.title = 'this is a title';
img.className += " profilePic"; // Add profilePic class to the image
var div = document.createElement("div");
div.className += " picWindow"; // Add picWindow class to the div
div.appendChild(img);
content.appendChild(div);
这篇关于使用classList的代码在IE中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文