问题描述
我有一个div,其中在某些标签上添加了动态数据属性.(数据属性的名称由脚本动态生成,因此我的脚本无法知道数据属性的名称)
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>
现在,我想编写一个代码,在其中删除所有数据属性及其值来重置div.
Now, I want to write a code in which it resets the div by removing all the data attributes and their values.
我可以使用
$('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.
删除div并重新插入div是不可行的.请帮助.预先感谢
removing div and reinserting div is not an option.Pls help.thanks in advance
推荐答案
您可以这样使用
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]);
}
修改
@Mottie建议
$.each($('div').data(), function (i) {
$("div").removeAttr("data-" + i);
});
这篇关于删除元素的所有动态“数据属性"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!