1

当我注意到一些奇怪的行为时,我正在开发一个 Web 应用程序。我有一个通过 JavaScriptstyle属性应用样式的元素。之后,我尝试删除应用在元素上的所有样式removeAttribute("style")。这仅适用于 Gecko。WebKit 什么都不做。

我发现了一种解决方法(setAttribute("style", "")在删除属性之前使用),但我不明白为什么setAttribute在 WebKit 而不是 Gecko 上需要它。为什么?

我在这里有一个行为示例。尝试注释掉这一setAttribute行,看看 Gecko 和 WebKit 之间的行为有何不同。

4

1 回答 1

0

这可能取决于您如何设置属性?

var test=document.getElementById("test");
//test.style.background="green";
test.setAttribute("style", "background: green");
test.removeAttribute("style");

我注释掉第二行,因为它是改变特定属性的不同方式。

现在第四行在 webkit 中正常工作(使用 google chrome dev 频道),当我注释掉它时,//test.removeAttribute("style")第三行的框保持绿色。

于 2011-03-27T01:11:44.153 回答