Style behavior difference between WebKit and Gecko

走远了吗. 提交于 2019-12-24 10:52:04

问题


I was working on a web application when I noticed some peculiar behavior. I have an element with styles applied via the JavaScript style property. Afterwards, I tried to remove all of the styles applied on the element with removeAttribute("style"). This only works on Gecko. WebKit does nothing.

I have discovered a workaround (using setAttribute("style", "") before removing the attribute) but I don't understand why the setAttribute would be needed on WebKit but not Gecko. Why?

I have an example of the behavior here. Try commenting out the setAttribute line and see how the behavior differs between Gecko and WebKit.


回答1:


Could it depend on how you set the attribute?

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

I comment out the second line, because it is a different way of changing that particular attribute.

Now the fourth line works correctly in webkit (using google chrome dev channel), and when I comment it out, //test.removeAttribute("style"), the box remains green from the third line.



来源:https://stackoverflow.com/questions/5446486/style-behavior-difference-between-webkit-and-gecko

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!