Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Basic question.

document.getElementById("yy").onmouseover = hi;
//document.getElementsByTagName("li").onmouseover = hi;
...

In this example, http://jsfiddle.net/8fURz/1/ why does the first line work, but not the second line (when it is un-commented, of course)?

I know I can do this easily with jQuery, just wonderin...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
573 views
Welcome To Ask or Share your Answers For Others

1 Answer

Because document.getElementsByTagName("li") return a NodeList, you need to bind event handler to every element of the NodeList.

var list = document.getElementsByTagName("li");
for (var i= 0; i < list.length; i++) {
  list[i].onmouseover = hi;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...