jQuery.data和动态更改HTML5自定义属性 [英] jQuery.data and dynamically changing HTML5 custom attributes
问题描述
jQuery对象html5自定义属性正在缓存DATA。
在我的应用程序中,我有一个表单,其中包含一个具有不断变化的自定义数据属性的字段,这种特定的行为对于表单的功能是必不可少的。
jQuery objects html5 custom attributes DATA is being cached.
In my application I have a form with a field that has a changing custom data attribute, and this specific behavior is imperative for the functionally of the form.
有一个带有一些默认自定义属性的输入字段:
There is an input field with some default custom attribute:
<input type="text" name="xxx" data-test="4">
$('input')。data()
结果将是 {test =4}
$('input')。attr('data-test','5')
表示 $('input')。data()
结果 STILL {test = 4}
我怎样才能始终确保获得所有REAL自定义属性,元素上可以有多个,使用 $。data()
功能?我在每次获取之前尝试过 $。removeData()
,但它会从元素中完全清除所有数据,因此无法再访问它。
How can I always make sure to get all the REAL custom attributes, there can be more than one on an element, using the $.data()
function? I have tried the $.removeData()
before each fetch, but it cleans all the data completely from the element so it isn't accessible any longer.
推荐答案
你需要这样做: $('input')。data('test',5)
如果你打电话给 .attr('test',5)
你将设置一个属性,你正在影响它< input type ='text'data-test ='4'test ='5'/>
If you call .attr( 'test' , 5 )
you will be setting an attribute, and you are affecting it like this <input type='text' data-test='4' test='5' />
作为一个注释,您可以获得如下特定数据属性: var test = $('input')。data('test');
As a note, you can get specific data attributes like this: var test = $('input').data('test');
编辑
要删除特定数据属性,您可以执行此操作这个: jQuery.removeData($('input'),test);
For removing a specific data attribute, you can do this: jQuery.removeData( $( 'input' ) , "test" );
这篇关于jQuery.data和动态更改HTML5自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!