gpt4 book ai didi

javascript - 带有 classList 的代码在 IE 中不起作用?

转载 作者:IT王子 更新时间:2023-10-29 02:48:55 25 4
gpt4 key购买 nike

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

Unable to get value of the property 'add': object is null or undefined"

我认为这只是一个 IE 支持问题。您如何使以下代码在 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 += ".."。注意:不要省略空格:类名应添加到以空格分隔的列表中。

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);

关于javascript - 带有 classList 的代码在 IE 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8098406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com