質問

I am trying the following code and getting following results:

console.log(document.getElementsByTagName('a')[0]); //retuns <a id="link" href="http://stackoverflow.com/questions/ask"></a>

console.log($('a')[0]);//retuns <a id="link" href="http://stackoverflow.com/questions/ask"></a>

console.log(document.getElementById('link'));//retuns <a id="link" href="http://stackoverflow.com/questions/ask"></a>

console.log(document.getElementById('link')[0]);// returns undefined 

console.log($('#link')[0]); //retuns <a id="link" href="http://stackoverflow.com/questions/ask"></a>

Can anybody tell why these (console.log(document.getElementById('link')[0]);// returns undefined and console.log($('#link')[0]); //retuns <a id="link" href="http://stackoverflow.com/questions/ask"></a>) behave weirdly?

Demo

役に立ちましたか?

解決

getElementsByTagName (plural) returns an array.

getElementById (singular) returns a single element (or null).

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top