微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.jb51.cc/jquery/182257.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐