JavaScript   发布时间:2022-04-16  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了javascript focus()不专注大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
常规文本字段,用户输入字符串.测试a)输入中是否有东西,b)输入中没有空格,C)只是整数,没有其他字符.
然后是提交按钮.
你会注意到我没有使用html行为,输入中没有onclick,严格的内容/表示/行为分离.

我的HTML

<form name="basicText" id="basicText" action="">
<p>Enter any ol' Integer: 
<input type="text" id="inputBox" name="inputBox" size="14"/>    
<input value = "Next...?" type="submit" id="mySubmitBtn" name="mySubmitBtn"/>
</p>
</form>
<script src="js/w1bscript.js" type="text/javascript"></script>

另请注意,外部javascript文件在末尾添加,因此所有元素都可以加载(现在不用担心onload).

JavaScript:

var myButton1 = document.getElementById("mySubmitBtn");
var myForm1 = document.getElementById("basicText");
var myTextBox = myForm1.inputBox;

function submitPress() {
 if(myTextBox.value.length == 0) {
    alert("You didn't enter anything! Once more time,with feeling...")
    basicText.focus();
    basicText.SELEct();
 } else if(/\s/.test(myTextBox.value)) {
    alert("Eh... No spaces. Just Integers. Try again...");
    basicText.focus();
    basicText.SELEct();
  } else if (isNaN(myTextBox.value)==truE) {
    alert("Eh... Gonna need you to enter ONLY digits this time. kthnx.");
    basicText.focus();
    basicText.SELEct();
 } else {
    // The textbox isn't empty,and there's no spaces. Good.
    alert("you've passed the test!")
  }
}
myButton1.addEventListener('click',submitPress,falsE);

当我输入错误的输入时,逻辑工作,但无论我使用什么浏览器,光标都不会聚焦回文本框.

小提琴:http://jsfiddle.net/2CNeG/

谢谢,

解决方法

您将观察到,一旦您对警报框进行了评论,您将能够看到您的焦点正在工作.但是有一个解决方案.试试这个.我认为它应该有效.
setTimeout(function() {
      document.getElementById('inputBox').focus();
    },0);

大佬总结

以上是大佬教程为你收集整理的javascript focus()不专注全部内容,希望文章能够帮你解决javascript focus()不专注所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。