removeData()jQuery方法不起作用 [英] removeData() jquery method not working

查看:70
本文介绍了removeData()jQuery方法不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我正在正确使用removeData,但它似乎无法正常工作,这是我在开发控制台中看到的内容,有人可以解释我在做什么错吗?

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.

$('.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

推荐答案

这是因为您的data源自HTML data-fieldlength属性.根据文档:

It's because your data originates in the HTML data-fieldlength attribute. According to the docs:

使用.removeData("name")时,jQuery将尝试查找数据- 如果元素上没有该名称的属性,则该元素上的属性 内部数据缓存.为了避免重新查询data-属性,请设置 名称为空值或未定义的值(例如.data("name", 未定义)),而不是使用.removeData().

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

这篇关于removeData()jQuery方法不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆