2011-01-25 171 views
5

我有一個文本框,我想設置焦點,但它不起作用。Javascript焦點()功能不工作

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

任何想法?

+0

該文本框的html是什麼樣子的? – 2011-01-25 20:42:24

+5

是`document.getElementById(「txtCity」)`返回undefined`嗎?控制檯中的任何JavaScript錯誤?什麼瀏覽器?元素是否實際可聚焦? **顯示更多代碼。** – 2011-01-25 20:43:23

+0

使用IE8。頁面上沒有錯誤。 – 2011-01-25 20:44:38

回答

6

也許您在輸入元素呈現之前調用JavaScript?將輸入元素放在JavaScript之前,或等到頁面加載完成後再觸發JavaScript。

的順序,它工作得很好:

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

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> 
-1

嘗試將其包裝到文檔r中eady函數並確保你有jquery包含。

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

2

我也面臨着同樣的problem.To解決此問題,將代碼放在setTimeout函數。

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