jQuery .data()和setAttribute之间的区别 [英] Difference between jQuery .data() and setAttribute
问题描述
1)在设置用户数据时(请参见实时演示): /p>
1) When setting user data (see Live demo) with :
document.getElementById('myelement1').setAttribute('data-size', 20);
当用浏览器的检查器分析元素时,我可以看到这一点:
I can see this when analyzing the element with the browser's inspector :
<div id="myelement1" data-size="20">Blah</div>
2)使用jQuery设置用户数据时:
2) When setting user data with jQuery :
$('#myelement2').data('size', 20);
当用浏览器的检查器分析元素时,我可以看到这一点:
I can see this when analyzing the element with the browser's inspector :
<div id="myelement2">Blah</div>
为什么元素在1)中出现在DOM中,但在2)中不存在?
jQuery .data('size', 20)
如何存储数据,以及数据是否不在DOM中?
(我在JS中不够先进,无法在执行此操作的代码中找到确切的位置)
How does jQuery .data('size', 20)
store the data, and where if it's not in the DOM ?
(I'm not advanced enough in JS to find the exact place in the code that does that)
推荐答案
jQuery不会将物理属性附加到元素.它保留一个内部对象(称为cache
),并在其中保留数据,并引用与其对应的元素-它不存储在DOM或属性中.
jQuery doesn't attach a physical attribute to the element. It keeps an internal object (called cache
) where it keeps the data, with a reference to the element that it corresponds to - it isn't stored in the DOM or an attribute.
这篇关于jQuery .data()和setAttribute之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!