当我注意到一些奇怪的行为时,我正在开发一个网络应用程序。我有一个通过 JavaScript style
属性应用样式的元素。之后,我尝试使用 removeAttribute("style")
删除元素上应用的所有样式。这仅适用于 Gecko。 WebKit 什么都不做。
我发现了一个解决方法(在删除属性之前使用 setAttribute("style", "")
)但我不明白为什么需要 setAttribute
在 WebKit 但不是 Gecko。为什么?
我有一个行为的例子 here . 尝试注释掉 setAttribute
行,看看 Gecko 和 WebKit 之间的行为有何不同。
最佳答案
这是否取决于您如何设置属性?
var test=document.getElementById("test");
//test.style.background="green";
test.setAttribute("style", "background: green");
test.removeAttribute("style");
我注释掉了第二行,因为这是更改特定属性的不同方式。
现在第四行在 webkit 中正常工作(使用 google chrome dev channel ),当我注释掉它时,//test.removeAttribute("style")
,该框从第三行。
关于javascript - WebKit 和 Gecko 之间的样式行为差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5446486/