What is the implementation of GetElementByID()?

前端 未结 3 1279
故里飘歌
故里飘歌 2021-01-21 08:00

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 intelligen

3条回答
  •  说谎
    说谎 (楼主)
    2021-01-21 08:34

    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.

提交回复
热议问题