2015-04-18 53 views
0

这是我简单的代码,但它似乎不工作。 我已经阅读了很多Q & A的,但我不知道我做错了什么?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 focus(){ 
      $("#input").focus(); 
     } 
    </script> 
</head> 
<body> 
    <button onClick="focus();">button</button> 
    <input id="input"> 
</body> 
</html> 

我确定有一些我失踪,但我不知道它是什么,我想在这方面提供任何帮助。

谢谢。

回答

1

重命名你对焦功能,它与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

+0

哇!谢谢!!! – nathan

2

的问题是功能name.change重点不存在于任何其他名称library.in jquery库中有一个名为focus的函数,你也将函数名称声明为焦点。

function myFunc(){ 
     $("#input").focus(); 
} 

<button onClick="myFunc();">button</button> 

看看这个:

https://jsfiddle.net/onq2jgjo/

+0

感谢您的回复,即时通讯不尝试更改任何内容,即时尝试让文本光标出现在输入按钮被点击时 – nathan

+0

Serlkhan说同样的请试着理解他的答案。 – Sumit

+0

好吧,谢谢,现在通过阅读下面的答案可以理解,谢谢你们,这一直是我的头 – nathan

相关问题