我知道在stackoverflow上有類似的問題很多,但他們都沒有解決我的問題。我正在嘗試構建一個小型的「金字塔」文字遊戲。你必須找到一個韓文單詞的正確翻譯,然後一個新的單詞將會彈出一個新的輸入。解決這個問題的話,下一個輸入字段會彈出,...focus()在輸入(所有瀏覽器)中都不起作用
HTML:
<input id="ha_1" class="halter" placeholder="안녕하세요" type="text" onblur="checkSol(id);"></input><br/>
<input id="ha_2" class="halter" placeholder="얼굴" type="text" onblur="checkSol(id);"></input><br/><br/>
<input id="ha_3" class="halter" placeholder="문" type="text" onblur="checkSol(id);"></input><br/><br/>
下面是相關的代碼:
// compare if user input is correct solution
if (user_input == solution){
// if correct, display the next input field:
$('#' + e_dies).nextAll('.halter:first').css('display', 'block');
// ==> supposed to focus the fadedIn input
$('#next_input').focus();
// count up to the next word
l_count++;
// give correct/incorrect feedback
if(l_count == last_sol){
tell_me.innerHTML = 'Correct';
return false;
};
} else if(document.getElementById(e_dies).value == "") {
tell_me.innerHTML = '';
}
這裏這條線$('#' + e_dies).nextAll('.halter:first').css('display', 'block');
顯示一個輸入領域。之後,$('#next_input').focus();
應該專注於此輸入字段,它只是淡入。我嘗試了一些解決方案,如使用setTimeout
或將其移動到函數的末尾。沒有爲我工作。
奇怪的是,其他命令如$('#next_input').css('color', 'red');
工作得很好,只是.focus()
惹上麻煩。
幫助將不勝感激!
請檢查瀏覽器兼容性http://caniuse.com/#search=focus – Rajesh
似乎沒有提到JS'focus()'方法。我正在使用最新版本的Firefox,Chrome和邊緣。在其他例子中,'.focus()'爲我工作,所以它必須是我的代碼內的問題,我猜。不知道爲什麼元素的其他更改可以工作(如顏色更改),但不是焦點 – sojutyp
此解決方案針對 – Vivek