2015-04-18 54 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

相關問題