【问题标题】:focus() doesn't work in input (all browsers)focus() 在输入中不起作用(所有浏览器)
【发布时间】: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_input id 的元素在哪里?
  • 你不需要一个结束输入标签&lt;/input&gt;,如果可能的话为你的代码提供完整的html,很难根据假设给出答案。

标签: javascript jquery


【解决方案1】:

虽然问题似乎不太清楚,但我已经实现了一个小小提琴,显示输入框焦点在条件值上工作。我希望这可以提供一些帮助

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>

function checkSol(id){
	var solution="testname";
  console.log(event);
  var user_input=event.target.value;
  if (user_input == solution){
// if correct, display the next input field:
            
    // ==> supposed to focus the fadedIn input
             $('#focusinputbox').focus();

  }
}
</script>

<input id="ha_1" class="halter" placeholder="안녕하세요"  type="text" onblur="checkSol(id);" /><br/>
<input id="ha_2" class="halter" placeholder="얼굴" type="text" onblur="checkSol(id);" /><br/><br/>
<input id="ha_3" class="halter" placeholder="문" type="text" onblur="checkSol(id);" /><br/><br/>
<input id ='focusinputbox' placeholder='inputtobefocussed'/>

【讨论】:

  • 你的脚本杀死我浏览器的tab
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-12
  • 2018-08-16
  • 1970-01-01
  • 2017-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多