【问题标题】:jQuery `.focus()` not workingjQuery`.focus()`不起作用
【发布时间】:2015-04-18 19:54:05
【问题描述】:

这是我的简单代码,但它似乎不起作用。 我读了很多问答,但我不知道我做错了什么?

<!DOCTYPE html>
<head>
    <title>Test</title>
    <script     src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script>
        function focus(){
            $("#input").focus();
        }
    </script>
</head>
<body>
    <button onClick="focus();">button</button>
    <input id="input">
</body>
</html>

我确定我缺少某些东西,但我不知道它是什么,我需要这方面的任何帮助。

谢谢。

【问题讨论】:

    标签: jquery html input focus


    【解决方案1】:

    问题是函数名。将焦点更改为库中不存在的任何其他名称。在 jquery 库中有名为焦点的函数,您也将函数名声明为焦点。

    function myFunc(){
            $("#input").focus();
    }
    
     <button onClick="myFunc();">button</button>
    

    看看这个:

    https://jsfiddle.net/onq2jgjo/

    【讨论】:

    • 感谢您的回复,我没有尝试更改任何名称,我试图在单击按钮时让文本光标出现在输入中
    • Serlkhan 也是这么说的,请试着理解他的回答。
    • 好的,谢谢,现在通过阅读下面的答案理解了,谢谢大家,这一直在我的脑海中
    【解决方案2】:

    重命名你的焦点函数,它与jquery内置函数focus()冲突

        <!DOCTYPE html>
    <head>
        <title>Test</title>
        <script     src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <script>
            function focus1(){
                $("#input").focus();
            }
        </script>
    </head>
    <body>
        <button onClick="focus1();">button</button>
        <input id="input">
    </body>
    </html>
    

    DEMO

    【讨论】:

      猜你喜欢
      • 2017-12-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-23
      • 1970-01-01
      • 1970-01-01
      • 2010-11-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多