本文介绍了removeAttribute()不工作DOM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 removeAttribute()删除以下代码中的任何内容:

Why doesn't removeAttribute() remove anything in the following code:

<!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>


推荐答案

问题是您正在进行所有这些更改在DOM更新之前。相反,请考虑将style属性设置为无,请参阅

The problem is you are making all these changes at once before the DOM updates. Instead consider actually setting the style attribute to nothing see here

        div.setAttribute("style","");

这篇关于removeAttribute()不工作DOM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 04:59