removeData() jquery 方法不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16615383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
removeData() jquery method not working
提问by turbo2oh
I think I'm using removeData correctly but it doesn't seem to be working, here's what I'm seeing in the dev console, could anyone explain what I'm doing wrong?
我想我正在正确使用 removeData 但它似乎不起作用,这是我在开发控制台中看到的内容,有人能解释一下我做错了什么吗?
I'm outputting the current data attribute value, calling removeData, then outputting the value again and its still there.
我正在输出当前的数据属性值,调用 removeData,然后再次输出该值并且它仍然存在。
$('.questionList > li').eq(1).data('fieldlength')
3
$('.questionList > li').eq(1).removeData('fieldlength');
[
<li class=?"questionBox" data-createproblem=?"false" data-fieldlength=?"3" data-picklistvalues data-required=?"true" data-sfid=?"a04d000000ZBaM3AAL" data-type=?"Text">?
<div class=?"questionLabel">?Birthdate?</div>?
</li>?
]
$('.questionList > li').eq(1).data('fieldlength')
3
回答by Iain Collins
There is bit of gotcha I wanted to clarify in case anyone else stumbles upon it...
我想澄清一些问题,以防其他人偶然发现它......
If you have HTML5 data-*
attributes on an element you need to use jQuery's removeAttr()
instead of removeData()
if you want to remove them from the element in the DOM.
如果您data-*
在元素上具有 HTML5属性,则需要使用 jQueryremoveAttr()
而不是removeData()
如果您想从 DOM 中的元素中删除它们。
For example, to actually remove a data attribute from an element you need to use:
例如,要从元素中实际删除数据属性,您需要使用:
$({selector}).removeAttr('data-fieldlength');
You can readvalues like this with $({selector}).data('fieldlength')
but removeData()
doesn't actually remove them if they are HTML attributes on an element present in the source of the page (it just deletes it in memory, so that if you query it again with jQuery it appearsto have been removed).
您可以读取这样的值,$({selector}).data('fieldlength')
但removeData()
如果它们是页面源中存在的元素上的 HTML 属性,则实际上不会删除它们(它只是将其删除在内存中,因此如果您再次使用 jQuery 查询它似乎有已删除)。
Personally I think this behaviour is broken and I'm sure catches a lot of people out.
就我个人而言,我认为这种行为是错误的,我相信很多人都会被抓到。
回答by Blazemonger
It's because your data
originates in the HTML data-fieldlength
attribute. According to the docs:
这是因为您data
源自 HTMLdata-fieldlength
属性。根据文档:
When using .removeData("name"), jQuery will attempt to locate a data- attribute on the element if no property by that name is in the internal data cache. To avoid a re-query of the data- attribute, set the name to a value of either null or undefined (e.g. .data("name", undefined)) rather than using .removeData().
当使用 .removeData("name") 时,如果内部数据缓存中没有该名称的属性,jQuery 将尝试在元素上定位数据属性。为避免重新查询 data- 属性,请将名称设置为 null 或未定义的值(例如 .data("name", undefined)),而不是使用 .removeData()。
So instead of
所以代替
$('.questionList > li').eq(1).removeData('fieldlength');
you should do
你应该做
$('.questionList > li').eq(1).data('fieldlength',null);
回答by Sandesh Chavan
In Hidden Field using custom data attribute to store object data Should use .removeAttr() instead of .removeData() using ID
在隐藏字段中使用自定义数据属性来存储对象数据应该使用 .removeAttr() 而不是 .removeData() 使用 ID
回答by Gardoku
Actually this works better for me. Because it leaves the attribute intact in the element but with no assigned value to it.
实际上这对我来说效果更好。因为它使元素中的属性完好无损,但没有为其分配值。
$(selector).attr("data-fieldlength","");
回答by ztrat4dkyle
.removeData()
will only remove data from jQuery's internal .data()
cacheso any corresponding data-
attributes on the element will not be removed. A later call to data()
will therefore re-retrieve the value from the element's data-
attribute. To prevent this, use .removeAttr()
alongside .removeData()
to remove the data-
attribute as well.
.removeData()
将只从jQuery的内部删除数据.data()
高速缓存,所以任何对应data-
的元素的属性将不被删除。data()
因此,稍后调用将重新检索元素data-
属性中的值。为防止出现这种情况,还可以使用.removeAttr()
with.removeData()
来删除该data-
属性。
Example:
例子:
$('div').removeData('info');
$('div').removeAttr('data-info');
Then set either:
然后设置:
$('div').data('info', 222);
$('div').attr('data-info', 222);