removeAttribute() 不适用于 DOM

发布于 2024-11-30 22:22:33 字数 821 浏览 3 评论 0原文

为什么 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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

千笙结 2024-12-07 22:22:33

只需在removeAttribute(“style”)之前调用getAttribute(“style”)即可。

例如,

    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)
    }

请参阅 http://jsfiddle.net/qTv22/

这看起来非常像 Chrome JS 优化错误。尽管 HTML5 规范说

样式 IDL 属性必须返回一个 CSSStyleDeclaration,其值
表示属性中指定的声明(如果存在)。
改变 CSSStyleDeclaration 对象必须创建一个样式属性
在元素上(如果还没有),然后更改其值
是表示序列化形式的值
CSSStyleDeclaration 对象。每次必须返回相同的对象
时间。

Chrome 试图尽可能推迟样式属性的创建,以便 IDL 属性的一系列突变可以汇总到内容属性的单个创建/更改中。该代码只是省略了在调用removeAttribute之前执行创建/更改操作,但对getAttribute执行正确。创建内容属性后,removeAttribute 将成功销毁它。

Just call getAttribute("style") before removeAttribute("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/

This looks very much like a Chrome JS optimization bug. Although the HTML5 spec says

The style IDL attribute must return a CSSStyleDeclaration whose value
represents the declarations specified in the attribute, if present.
Mutating the CSSStyleDeclaration object must create a style attribute
on the element (if there isn't one already) and then change its value
to be a value representing the serialized form of the
CSSStyleDeclaration object. The same object must be returned each
time.

Chrome is trying to defer the creating of the style attribute for as long as it can, so that a series of mutations of the IDL attribute can be rolled up into a single creation/change of the content attribute. The code is simply omitting to perform the create/change action before the removeAttribute call, but does so correctly for getAttribute. Once the content attribute has been created, removeAttribute will successfully destroy it.

凝望流年 2024-12-07 22:22:33

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

        div.setAttribute("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","");
落日海湾 2024-12-07 22:22:33

正如MDN 上的建议,应该使用removeAttribute而不是设置为 null 或空字符串。

但它可能无法正常工作,如果您想要定位的不太标准的浏览器不支持 RAF,解决方案是使用 requestAnimationFrame 或 setInterval。

As advised on MDN, one should use removeAttribute over setting to null or empty string.

It may not work properly though and the solution is to use requestAnimationFrame or setInterval if the less standard browser you want to target doesn't support RAF.

谁许谁一生繁华 2024-12-07 22:22:33

看起来需要更多时间,示例

function DelStyle() {
    var div = document.getElementById("myDiv");
    console.log(div)
    div.style.border = 'solid #3B3B3D 1px';
    console.log(div)
    setTimeout((function(div) {
        return function() {
            div.removeAttribute("style");
            console.log(div);
        }
    })(div), 500);

}

我在半年后删除属性第二。

It looks like it needs a more time, example

function DelStyle() {
    var div = document.getElementById("myDiv");
    console.log(div)
    div.style.border = 'solid #3B3B3D 1px';
    console.log(div)
    setTimeout((function(div) {
        return function() {
            div.removeAttribute("style");
            console.log(div);
        }
    })(div), 500);

}

I'm removingAttribute after a half second.

网名女生简单气质 2024-12-07 22:22:33

测试一下

div.attributes.removeNamedItem("style");

Test this

div.attributes.removeNamedItem("style");
烛影斜 2024-12-07 22:22:33

我在使用removeProperty 和removeAttribute 时遇到问题。我通过在 html 而不是 css 中内联写入该属性/属性的原始实例来解决这个问题。

I was having trouble with removeProperty and removeAttribute. I solved it by writing the original instance of that property/attribute inline in my html instead of my css.

我纯我任性 2024-12-07 22:22:33

我尝试在调用 removeAttribute() 之前调用 setAttribute()getAttribute() 方法。

它对我不起作用:将样式属性设置为空也不起作用。

有效的是使用 removeAttributeNode() 方法。

如下所述: http://www.w3schools.com/jsref/met_element_removeattributenode.asp结果是一样的;例如

函数 DelStyle() {
    var div = document.getElementById("myDiv");
    控制台.log(div)
    div.style.border = '实心#3B3B3D 1px';
    控制台.log(div)
    div.removeAttributeNode(div.getAttribute("style"));
    控制台.log(div)
}

I tried calling both the setAttribute() and getAttribute() methods before calling removeAttribute().

It didn't work for me: Neither did setting the style attribute to nothing.

What did work was to use the removeAttributeNode() method.

As described here: http://www.w3schools.com/jsref/met_element_removeattributenode.asp the result will be the same; e.g.

function DelStyle() {
    var div = document.getElementById("myDiv");
    console.log(div)
    div.style.border = 'solid #3B3B3D 1px';
    console.log(div)
    div.removeAttributeNode(div.getAttribute("style"));
    console.log(div)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文