Javascript Expression "document.body.getElementsByTagName ();" Vs "document.getElementsByTagName ();"

Dear experts, I am completely new to javascript, and I often see that encoders use these interchangeabaly strings.

document.body.getElementsByTagName();

and

document.getElementsByTagName();

What is the difference, the benefits?

Does this have anything to do with FF and IE?

+3
source share
4 answers

Difference is context. In the first example, you are viewing the body tag, which means that you can never retrieve the body tag or any other elements outside this

In the second example example, you can get anything.

Nothing to do with specific browsers.

+7
source

, .. , . , , .

+1

, - .

, body.

, .

: , , .

:

document.getElementById('foo').getElementsByTagName('span')

,

document.getElementsByTagName('div')

, , DOM, , .

0

http://jsperf.com/document-body-getelementsbytagname1 says that ajm is incorrect,   document.getElementsByTagName('*');  works faster on FF!

0
source

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


All Articles