Does document.getElementById
JavaScript 返回一个实时 DOM 元素?出于性能原因我有兴趣知道
标准和“实时”之间的区别通常用于lists的元素。document.getElementById
返回对 DOM 节点的单个对象引用。一旦获取节点,引用将始终指向同一节点。
HTML for the example:
<div id="foo"></div>
JS for the example:
var foo,
bar;
foo = document.getElementById('foo'); //gets the div
bar = document.getElementById('bar'); //null
foo.setAttribute('id', 'bar');
console.log(foo.id); //'bar'
console.log(bar.id); //TypeError
引用不会仅仅因为元素的 ID 可能已更改而更新。
这与类似的东西形成对比document.getElementsByTagName
它返回具有给定标签的元素列表。当元素添加到 DOM 或从 DOM 中删除时,列表将自动更新。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)