Javascript Focus() 函数不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4798579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:09:52  来源:igfitidea点击:

Javascript Focus() function not working

javascript

提问by Itay.B

I have a textbox which I want to set the focus on, but it doesn't work.

我有一个要设置焦点的文本框,但它不起作用。

document.getElementById("txtCity").focus();

Any idea?

任何的想法?

回答by Martin Buberl

Maybe you are calling the JavaScript before the input element is rendered? Position the input element before the JavaScript or wait until the page is loaded before you trigger your JavaScript.

也许您在呈现输入元素之前调用了 JavaScript?将输入元素放在 JavaScript 之前,或者等到页面加载完毕后再触发 JavaScript。

In that order, it works just fine:

按照这个顺序,它工作得很好:

<input type="text" id="test" />
<script type="text/javascript">
  document.getElementById("test").focus();
</script>

In jQueryyou could place your code within the .ready()method to execute your code first when the DOM is fully loaded:

jQuery 中,您可以将代码放在.ready()方法中,以便在 DOM 完全加载时首先执行代码:

<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
  $(document).ready(function () {
    $("#test").focus();
    // document.getElementById("test").focus();
  });
</script>

回答by Rubi saini

I have also faced same problem.To resolve this problem, put your code in setTimeout function.

我也遇到了同样的问题。要解决这个问题,请将您的代码放在 setTimeout 函数中。

function showMeOnClick() {
    // Set text filed focus after some delay
    setTimeout(function() { jQuery('#searchTF').focus() }, 20);
    // Do your work.....
}

回答by kajo

Try to wrap it into document ready function and be sure, that you have jquery included.

尝试将其包装到文档就绪函数中,并确保包含 jquery。

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
    <script>
       $(document).ready(function() {
          $("#test").focus();
       });
    </script>

回答by asdf

    <div id="txtROSComments" contenteditable="true" onkeyup="SentenceCase(this, event)"style="border: 1px solid black; height: 200px; width: 200px;">
    </div>
    <script type="text/javascript">
        function SentenceCase(inField, e) {
            debugger;
            var charCode;

            if (e && e.which) {
                charCode = e.which;
            } else if (window.event) {
                e = window.event;
                charCode = e.keyCode;
            }

            if (charCode == 190) {
                format();
            }
        }

        function format() {
            debugger; ;
            var result = document.getElementById('txtROSComments').innerHTML.split(".");

            var finaltxt = "";


            var toformat = result[result.length - 2];

            result[0] = result[0].substring(0, 1).toUpperCase() + result[0].slice(1);

            if (toformat[0] != " ") {

                for (var i = 0; i < result.length - 1; i++) {
                    finaltxt += result[i] + ".";
                }

                document.getElementById('txtROSComments').innerHTML = finaltxt;
                alert(finaltxt);
                abc();
                return finaltxt;
            }



            if (toformat[0].toString() == " ") {
                debugger;
                var upped = toformat.substring(1, 2).toUpperCase();

                var formatted = " " + upped + toformat.slice(2);

                for (var i = 0; i < result.length - 1; i++) {

                    if (i == (result.length - 2)) {
                        finaltxt += formatted + ".";
                    }
                    else {
                        finaltxt += result[i] + ".";
                    }

                }
            }
            else {
                debugger;
                var upped = toformat.substring(0, 1).toUpperCase();

                var formatted = " " + upped + toformat.slice(1);



                for (var i = 0; i < result.length - 1; i++) {
                    if (i == (result.length - 2)) {
                        finaltxt += formatted + ".";
                    }
                    else {
                        //if(i
                        finaltxt += result[i] + ".";
                    }

                }

            }
            debugger;
            document.getElementById('txtROSComments').value = finaltxt;
            return finaltxt;

        }

    </script>
   <script type="text/javascript">
       function abc() {
           document.getElementById("#txtROSComments").focus();
       }

回答by Lance

It works fine in this example http://jsfiddle.net/lmcculley/rYfvQ/

它在这个例子中工作正常 http://jsfiddle.net/lmcculley/rYfvQ/