Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
IE和Firefox之间的Javascript差异_Javascript_Object Detection - Fatal编程技术网

IE和Firefox之间的Javascript差异

IE和Firefox之间的Javascript差异,javascript,object-detection,Javascript,Object Detection,我有以下几行Javascript代码: var button = document.getElementById("scriptsubmit"); button.setAttribute("class", "remove"); 在Firefox中,这一点非常有效,而在InternetExplorer中则不然 我知道InternetExplorer希望类是className,但我不确定如何检测使用哪个类,因为对象检测在这种情况下似乎不适用 感谢您的回复您可以在两种浏览器中直接使用classN

我有以下几行Javascript代码:

 var button = document.getElementById("scriptsubmit");
 button.setAttribute("class", "remove");
在Firefox中,这一点非常有效,而在InternetExplorer中则不然

我知道InternetExplorer希望类是className,但我不确定如何检测使用哪个类,因为对象检测在这种情况下似乎不适用


感谢您的回复

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

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

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

根据这些测试,IE中不完全支持
setAttribute()

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

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

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

除非我的Firefox版本被破坏了,否则这不是真的。这是真的,但你不能用“setAttribute”/“getAttribute”访问它……这不是我在这个问题上尝试的方式。事实上,你不应该在HTML文档上使用
getAttribute
/
setAttribute
。它在IE中有缺陷,并且不如使用DOM级别1 HTML属性(如
按钮.className
)可读。