IE和Firefox之间的Javascript差异
我有以下几行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
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
)可读。