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);

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: &lt;span id=&quot...
jQuery 添加水印 &lt;script src=&quot;../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...