[英]jQuery .data doesn't work when using a html jQuery object
I am having a problem using the .data
jQuery function for a HTML jQuery Object. 我在将
.data
jQuery函数用于HTML jQuery对象时遇到问题。
var test = $('<div><div class="abc123"></div></div>');
test.find('.abc123').data('please', 'change');
and test
still hasn't changed at all 而且
test
根本没有改变
<div>
<div class="abc123"><div>
</div>
But using the .text
function 但是使用
.text
函数
test.find('.abc123').text('TESTING');
works properly: 正常工作:
<div>
<div class="abc123">TESTING</div>
</div>
Is there any way to make the data
work on the test
var? 有什么方法可以使
data
在test
变量上工作?
Edit: 编辑:
What i am expecting after running 跑步后我期待什么
test.find('.abc123').data('please', 'change');
is 是
<div>
<div class="abc123" data-please="change"></div>
</div>
Calling .data()
on an element, whether created in HTML or in a JQuery object will not modify the associated markup. 在元素上调用
.data()
无论是在HTML中还是在JQuery对象中创建)都不会修改关联的标记。 Regardless, the associated DOM properties are updated. 无论如何,相关的DOM 属性更新。
var test = $('<div><div class="abc123"></div></div>');
test.find('.abc123').data('please', 'change');
alert(test.find('.abc123').data('please'));
The above code will alert 'change'. 上面的代码将警告“更改”。
If your goal is to also update the markup, you could instead use: 如果您的目标是还更新标记,则可以使用:
test.find('.abc123').attr('data-please', 'change');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.