GetElementByID()的实现是什么? [英] What is the implementation of GetElementByID()?
问题描述
是否遍历整个DOM树以找到指定的元素,或者是否足够智能以首先查找near-by元素?
谢谢
这取决于浏览器,但最可能使用id-> element的哈希映射。这是真的,有很多无效的页面有重复的ids。但是,浏览器仍然只返回一个元素,而不是一个集合。
我不知道你的意思是靠近元素,因为该方法只存在于文件
。
如果您有兴趣,可以找到免费软件浏览器(如Firefox和Chrome)的实现。
Can somebody please explain to me the internal implementation of HTML getElementById() method ? Is it traversing whole DOM tree to find the specified element or is it intelligent enough to look for near-by elements first ?
Thank you
It depends on the browser, but most probably use a hash map from id->element. It's true that there are many invalid pages that have duplicate ids. However, the browser will still only return one element, not a collection.
I don't know what you mean by "near by elements" since the method only exists on document
.
If you're interested, you can find the implementation for free software browsers such as Firefox and Chrome.
这篇关于GetElementByID()的实现是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!