我在做一個只使用JavaScript HTML和CSS的hang子手遊戲。 在我的一個函數中,我嘗試使用名爲value_的span屬性。 當程序到達這一點時,它只運行一次。當我點擊另一封信時,它什麼也沒做。如果我將getAttribute行放在註釋中,則該函數隨時可以運行,只需點擊一個字母即可。 任何想法? :\javascript getAttribute停止for循環
這是代碼:
//Search and Update function after click
function search_(target, letter)
{
for (var i = 0; i < randomWord.length; i++)
{
//Identify the <span>'s id by the letter
var target_ = document.getElementById(letter + i);
//Get the <span>'s value_
var attr_ = target_.getAttribute('value_');
alert(attr_);
/*if (randomWord[i] == attr_)
{
target_.className = 'hide';
};*/
};
};
'value_'屬性是否存在?我不這麼認爲,因爲它不是標準的HTML屬性。 'span'元素沒有'value',請使用'.textContent'代替。 – evolutionxbox
看看http://stackoverflow.com/questions/27240889/javascript-extract-span-value – evolutionxbox
當我使用textContent時,循環仍然在第二個循環上返回null。 編輯: 我給了它一個數據值屬性,但也是一樣的。 – Ben