删除所有动态'数据属性'元素的 [英] Removing all dynamic 'data attributes' of an Element
问题描述
我有一个 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]);
}
Edit
Suggested by @Mottie
$.each($('div').data(), function (i) {
$("div").removeAttr("data-" + i);
});
这篇关于删除所有动态&#39;数据属性&#39;元素的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!