【发布时间】:2017-03-22 09:43:13
【问题描述】:
我知道在 stackoverflow 上有很多类似的问题,但没有一个能解决我的问题。我正在尝试构建一个小型“金字塔”文字游戏。您必须为韩语单词找到正确的翻译,然后会弹出一个带有新单词的新输入。解决这个词,会弹出下一个输入框,...
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() 有麻烦。
我们将不胜感激!
【问题讨论】:
-
请检查浏览器兼容性caniuse.com/#search=focus
-
似乎没有提到 JS
focus()方法。我正在使用最新版本的 firefox、chrome 和 edge。在其他示例中,.focus()为我工作,所以我猜它必须是我的代码中的一个问题。不知道为什么对元素进行其他更改(例如颜色更改)但不是焦点 -
这个解决方案有什么作用
-
next_inputid 的元素在哪里? -
你不需要一个结束输入标签
</input>,如果可能的话为你的代码提供完整的html,很难根据假设给出答案。
标签: javascript jquery