繁体   English   中英

如何从div元素中删除data-属性?

[英]How to remove data- attribute from the div element?

我有一个html div元素,它包含许多HTML-5 data-属性来存储额外的

数据与元素。 我知道我们可以使用jquery的removeAttr删除特定的属性,

但我想知道有没有办法删除data-所有atonce?

不,你必须循环遍历它们并单独删除它们。 例如:

var $div = $("selector for the div"),
    attrs = $div[0].attributes,
    name,
    index;
for (index = attrs.length - 1; index >= 0; --index) {
    name = attrs[index].nodeName;
    if (name.substring(0, 5) === "data-") {
        $div.removeAttr(name);
    }
}

实时复制 | 资源

...或类似的规定。

不知道有任何批发方式这样做。 你必须用类似的东西遍历元素的属性

var el = document.querySelector('p');
for (var i=0; i<el.attributes.length; i++)
    if (/^data-/i.test(el.attributes[i].name))
        el.removeAttribute(el.attributes[i].name);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM