IE 和 Firefox 之间的 JavaScript 差异

发布于 2024-09-12 10:30:07 字数 295 浏览 3 评论 0原文

我有以下 Javascript 行:

 var button = document.getElementById("scriptsubmit");
 button.setAttribute("class", "remove");

在 Firefox 中,此功能完美运行,但在 Internet Explorer 中则不然。

我知道 Internet Explorer 期望类为 className,但我不确定如何检测要使用哪个,因为对象检测似乎不适用于这种情况。

感谢您的回复

I have the following lines of Javascript:

 var button = document.getElementById("scriptsubmit");
 button.setAttribute("class", "remove");

In Firefox this works perfectly and in Internet explorer it doesn't.

I am aware that Internet Explorer expects class to be className, but I'm uncertain how to detect which to use as object detection doesn't appear to apply in this case.

Thanks for your replies

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

勿忘初心 2024-09-19 10:30:07

您可以直接在两个浏览器中使用 className 属性:

var button = document.getElementById("scriptsubmit");
button.className = "remove";

You can just use the className property directly in both browsers:

var button = document.getElementById("scriptsubmit");
button.className = "remove";
注定孤独终老 2024-09-19 10:30:07

两种浏览器都支持 className,因此无需检测任何内容。

Both browsers support className, so there's no need to detect anything.

故笙诉离歌 2024-09-19 10:30:07

根据这些测试,IE 中不完全支持 setAttribute()
http://www.quirksmode.org/dom/w3c_core.html#t1110

解决这个问题的一种方法是创建一个新的 HTML 元素,设置它的属性,然后用它替换按钮,如下所示:

var newButton=document.createElement("button");
newButton.class="remove";

var oldButton=document.getElementById("button");
document.removeChild(oldButton);
document.appendChild(newButton);

According to these tests, setAttribute() is not fully supported in IE:
http://www.quirksmode.org/dom/w3c_core.html#t1110

One way to get around this is to create a new HTML element, set it's properties, then replace the button with it, like so:

var newButton=document.createElement("button");
newButton.class="remove";

var oldButton=document.getElementById("button");
document.removeChild(oldButton);
document.appendChild(newButton);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文