带有 classList 的代码在 IE 中不起作用? [英] Code with classList does not work in IE?

查看:22
本文介绍了带有 classList 的代码在 IE 中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码,但在 IE 中失败.消息是:

I'm using the following code, but having it fail in IE. The message is:

无法获取属性add"的值:对象为空或未定义"

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 属性不受 IE9 及更低版本支持.不过 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆