removeAttribute() 不适用于 DOM

Ala*_*Kid 5 javascript dom

为什么不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>
Run Code Online (Sandbox Code Playgroud)

Jos*_*kle 3

问题是您在 DOM 更新之前立即进行所有这些更改。相反,请考虑实际将样式属性设置为无,请参阅此处

        div.setAttribute("style","");
Run Code Online (Sandbox Code Playgroud)