removeData()jquery方法不工作

前端之家收集整理的这篇文章主要介绍了removeData()jquery方法不工作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我认为我正在使用removeData,但它似乎没有工作,这是我在开发控制台看到的,任何人都可以解释我做错了什么?

我正在输出当前的数据属性值,调用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

解决方法

这是因为您的数据源自HTML data-fieldlength属性。根据 the docs

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().

所以代替

$('.questionList > li').eq(1).removeData('fieldlength');

你应该做

$('.questionList > li').eq(1).data('fieldlength',null);
原文链接:https://www.f2er.com/jquery/182257.html

猜你在找的jQuery相关文章