How to add event listener for html class?

后端 未结 5 854
旧巷少年郎
旧巷少年郎 2021-01-12 01:14

If I have HTML like this:

5条回答
  •  清歌不尽
    2021-01-12 01:51

    No tags with classname preview so

    var preview = document.getElementsByClassName("preview");
    

    will cause that error

    this line must be as follows

    var preview = document.getElementsByClassName("previewBulk");
    

    and as @raym0nd said the number of div tags must be equal to the number of a tags

    EDIT

    the problem with your code is you used the index of for loop inside the anonymous function but this function is called with mouseout and mouseover event and after finishing the for loop try the following

    var movie = document.getElementsByClassName("movieImg");
    
        for(var i = 0, j=movie.length; i

    or test in jsFiddle

提交回复
热议问题