为什么不removeAttribute()
删除以下代码中的任何内容:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>test</title>
</head>
<body>
<div id="myDiv">
Element with style.
</div>
<br />
<br />
<button onclick="DelStyle()">
Remove the style attribute from the element
</button>
<script type="text/javascript">
function DelStyle() {
var div = document.getElementById("myDiv");
console.log(div)
div.style.border = 'solid #3B3B3D 1px';
console.log(div)
div.removeAttribute("style");
console.log(div)
}
</script>
</body>
</html>
只需在removeAttribute(“style”)之前调用getAttribute(“style”)即可。
e.g.
function DelStyle() {
var div = document.getElementById("myDiv");
console.log(div)
div.style.border = 'solid #3B3B3D 1px';
console.log(div)
div.getAttribute("style");
div.removeAttribute("style");
console.log(div)
}
See http://jsfiddle.net/qTv22/ http://jsfiddle.net/qTv22/
这看起来很像 Chrome JS 优化错误。尽管 HTML5 规范说
style IDL 属性必须返回一个 CSSStyleDeclaration,其值
表示属性中指定的声明(如果存在)。
改变 CSSStyleDeclaration 对象必须创建一个样式属性
在元素上(如果还没有),然后更改其值
是表示序列化形式的值
CSSStyleDeclaration 对象。每次必须返回相同的对象
时间。
Chrome 试图尽可能推迟样式属性的创建,以便 IDL 属性的一系列突变可以汇总到内容属性的单个创建/更改中。该代码只是省略了在调用removeAttribute之前执行创建/更改操作,但对getAttribute执行正确。创建内容属性后,removeAttribute 将成功销毁它。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)