简体   繁体   English

focus()在输入中不起作用(所有浏览器)

[英]focus() doesn't work in input (all browsers)

I know there is tons of questions like that on stackoverflow but none of them solved my problem. 我知道在stackoverflow上有很多类似的问题,但是没有一个问题解决了我的问题。 I'm trying to build a small "pyramid" word game. 我正在尝试构建一个小的“金字塔”文字游戏。 You have to find the right translation for a korean word, then a new input with a new word will pop up. 您必须为朝鲜语单词找到正确的翻译,然后弹出带有新单词的新输入。 Solve this word, the next input field will pop up, ... 解决这个词,将弹出下一个输入字段,...

HTML: 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/>

Here is the relevant piece of code: 这是相关的代码段:

// 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 = '';
        }

This line here $('#' + e_dies).nextAll('.halter:first').css('display', 'block'); 这行代码$('#' + e_dies).nextAll('.halter:first').css('display', 'block'); displays a input field. 显示一个输入字段。 Right after, $('#next_input').focus(); $('#next_input').focus();其后的是$('#next_input').focus(); is supposed to focus this input field , which just faded in. I tried some solutions like using setTimeout or moving it to the end of the function. 应该专注于刚刚淡入的这个输入字段。我尝试了一些解决方案,例如使用setTimeout或将其移至函数末尾。 Nothing worked for me. 什么都没有为我工作。

Strangely, other commands like $('#next_input').css('color', 'red'); 奇怪的是,其他命令如$('#next_input').css('color', 'red'); work just fine, just the .focus() makes trouble. 工作很好,只是.focus()麻烦了。

Help would be much appreciated! 帮助将不胜感激!

Though the question seems to be not that clear i have implemented a small fiddle showing input box focus working on conditional value.I hope this can give some help 虽然问题似乎还不清楚,但我实现了一个小提琴,显示输入框专注于条件值。我希望这会有所帮助

 <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'/> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM