What is the implementation of GetElementByID ()?

Can someone explain to me the internal implementation of the HTML getElementById () method? Is this a move of the entire DOM tree to find the specified element, or is it smart enough to first look for elements nearby?

thank

+3
source share
3 answers

It depends on the browser, but most likely uses the hash map from id-> element. It is true that there are many invalid pages with duplicate identifiers. However, the browser will still return only one item, not a collection.

I do not know what you mean by "next to the elements", since the method exists only on document.

, , Firefox Chrome.

+7

(er) . hashmap , ( , id , , id s). Internet Explorer 6 7 getElementById id, , Microsoft , IE8 IE9 .

, , , . WebKit Mozilla ( , , getElementById GetElementByIdInternal). fooobar.com/questions/167555/.... ( , , , , , , , ...)

+5

. -, , , .

, Mozilla Firefox, , .

+1
source

Source: https://habr.com/ru/post/1783080/


All Articles