GetElementByID()的实现是什么? [英] What is the implementation of GetElementByID()?

查看:264
本文介绍了GetElementByID()的实现是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向我解释HTML 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆