javascript - WebKit 和 Gecko 之间的样式行为差异

标签 javascript html css webkit gecko

当我注意到一些奇怪的行为时,我正在开发一个网络应用程序。我有一个通过 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/

相关文章:

javascript - Uncaught ReferenceError : require is not defined with Dojo API

html - 如何用绝对定位和相对定位正确定位

javascript - 为什么我的 Bootstrap 按钮在窗口缩小时重叠

javascript - Ng-Paste - 以数组或列表的形式访问粘贴的数据

javascript - 正则表达式替换输入字段中的符号

javascript - Discord bot 间隔困惑

php - 从 URL 读取 id

javascript - useState 和 useEffect 之间的区别

javascript - 关闭(忽略)html 文本/搜索元素中的占位符属性

html - Box-shadow 在 Chrome 中是黑色的,但在 Firefox 中是白色的?