get the tag name of object returned by getElementsByName
so suppose I call document.getElementsByName("title");
in javascri开发者_运维百科pt
and I want to know the type of the tag of the element that is returned by that function, for instance, to see if it's a meta tag or a div tag or a span tag, etc
how would I go about doing this?
document.getElementsByName("title");
returns a set of elements not a single element
so within a cycle you could use element.tagName
to get the tag
basicly
document.getElementsByName("title")[0].tagName
should work
You have returned a NodeList
object, so you would need to be more specific with your selector, or choose the first element using an index of 0, as in the other answers.
Whilst you can use nodeName
or tagName
, nodeName
is the better option.
You can use the tagName
property like so:
document.getElementsByName('name')[index].tagName;
You need the index as the getElementsByName
function returns an array.
精彩评论