删除所有动态'数据属性'元素的 [英] Removing all dynamic 'data attributes' of an Element

查看:23
本文介绍了删除所有动态'数据属性'元素的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 div,其中在某些标签上添加了动态数据属性.(data-attribute 的名称由脚本动态生成,所以我的脚本无法知道 data-attribute 的 NAME)

<div data-1223="some data" data-209329="some data" data-dog="some value">

现在,我想编写一个代码,通过删除所有数据属性及其值来重置 div.

我可以使用

$('div').attr('data-209329',"")

但问题是我不知道将要添加的数据属性的名称.将添加的数据属性是动态的,我无法控制.

删除 div 并重新插入 div 不是一种选择.请帮忙.提前致谢

解决方案

你可以这样使用

var data = $("div").data();var 键 = $.map(data, function (value, key) {返回键;});for (i = 0; i 

小提琴

编辑

@Mottie 推荐

$.each($('div').data(), function (i) {$("div").removeAttr("data-" + i);});

演示

I have a div where dynamic data- attributes get added on some tags. (name of data- attributes to be generated dynamically by script, so there is no way my script will know the NAME of data-attribute)

<div data-1223="some data" data-209329="some data" data-dog="some value"> </div>

Now, I want to write a code in which it resets the div by removing all the data attributes and their values.

I can use

$('div').attr('data-209329',"")

but the problem is I don't know the name of data-attribute that will be added. the data-attribute that will be added is dynamic and I don't have control of it.

removing div and reinserting div is not an option. Pls help. thanks in advance

解决方案

YOu can use like this

var data = $("div").data();

var keys = $.map(data, function (value, key) {
    return key;
});
for (i = 0; i < keys.length; i++) {
    $("div").removeAttr("data-" + keys[i]);
}

Fiddle

Edit

Suggested by @Mottie

$.each($('div').data(), function (i) {
    $("div").removeAttr("data-" + i);
});

Demo

这篇关于删除所有动态&amp;#39;数据属性&amp;#39;元素的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆