我有这个脚本,我正在运行。它显示在第164行未定义getAttribute属性。我不明白为什么。我已经尝试回溯各种函数调用以寻找中断,但我没有看到它
答案 0 :(得分:0)
您的mouseleave
事件监听器总是会event.target.className
album-view-song-item
因为songRows
被定义为document.getElementsByClassName("album-view-song-item");
因此,在侦听器中调用getSongItem(event.target)
时:
songRows[i].addEventListener("mouseleave", function(event) {
var songItem = getSongItem(event.target);
var songItemNumber = songItem.getAttribute("data-song-number");
if (songItemNumber !== currentlyPlayingSong){
songItem.innerHTML = songItemNumber;
}
});
element.className
是album-view-song-item
。当这被传递到你的函数时:
var getSongItem = function(element){
switch(element.className){
case "album-song-button":
case "ion-play":
case "ion-pause":
return findParentByClassName(element, "song-item-number");
case "song-item-title":
case "song-item-duration":
return findParentByClassName(element, "album-view-song-item").querySelector(".song-item-number");
case "song-item-number":
return element;
default:
return;
}
};
这将落到default
的情况,返回undefined。如您所见,在未定义的情况下调用getAttribute
会向您显示错误Cannot read property 'getAttribute' of undefined