在下面的findParentByClassName函數中,currentParent返回undefined。有人能告訴我爲什麼嗎?我認爲它實際上在getSongItem,clickHandler和HTML TableRowElement中也是未定義的。爲什麼currentParent未定義
這是問題出現的地方。
var findParentByClassName = function(element, targetClass) {
if (element) {
if (element.parentElement && element.parentElement.className) {
if (element.parentElement === null) {
console.log("No parent found");
} else if (element.parentElement.className !== targetClass) {
console.log("No parent found with that class name.");
} else if (element.parentElement !== null &&
element.parentElement.className === targetClass) {
var currentParent = element.parentElement;
}
while (currentParent.className !== targetClass && currentParent.className !==
null) {
currentParent = currentParent.parentElement;
}
//I need to know why currentParent is returning undefined
return currentParent;
}
}
};
這裏也可能有問題。
var getSongItem = function(element) {
switch (element.className) {
case 'album-song-button':
case 'ion-play':
case 'ion-pause':
return findParentByClassName(element, 'song-item-number');
case 'album-view-song-item':
return element.querySelector('.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;
}
};
你可以在問題中包含HTML嗎? – guest271314
我不知道如何在創建原始帖子之後。 –
您可以在stacksnippets中重現該問題嗎?見https://stackoverflow.com/help/mcve – guest271314