开发者

What exactly does this javascript getElementsByTagName syntax do?

开发者 https://www.devze.com 2023-01-13 05:52 出处:网络
I have this: var targetTitle = targetElement.getElementsByTagName(\'title\').item(0); Am i passing a plain string into targetTitle? or what am i passing exactly?

I have this:

var targetTitle = targetElement.getElementsByTagName('title').item(0);

Am i passing a plain string into targetTitle? or what am i passing exactly?

Which element would be item(1), item(2), etc... in here:

<title>title1</title>
<title><title2</title>

Does it just look for ALL the title tags on the page and return the 0,1,2/?

Would this return 'title2':

targetElement.getElementsB开发者_Go百科yTagName(‘title’).item(1)


The targetTitle will contain a reference to the DOM element object for the title tag.

The code only gets the first title tag, as there is only one in each document.


You're getting the first <title> element from targetElement.

item(0): <title>title1</title>
item(1): <title><title2</title>

Note: those indexes starts with zero, not one.

0

精彩评论

暂无评论...
验证码 换一张
取 消