2011-01-25 46 views
5

Tôi có một hộp văn bản mà tôi muốn đặt trọng tâm vào, nhưng nó không hoạt động.Chức năng Javascript Focus() không hoạt động

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

Bất kỳ ý tưởng nào?

+0

html cho hộp văn bản trông như thế nào? –

+5

Là 'document.getElementById (" txtCity ")' return' undefined'? Bất kỳ lỗi JavaScript nào trong bảng điều khiển? Trình duyệt nào? Phần tử có thực sự tập trung không? ** Hiển thị thêm mã. ** –

+0

Sử dụng IE8. Không có lỗi trên trang. –

Trả lời

6

Có thể bạn đang gọi JavaScript trước khi phần tử đầu vào được hiển thị? Định vị phần tử đầu vào trước JavaScript hoặc đợi cho đến khi trang được tải trước khi bạn kích hoạt JavaScript của mình.

Để đó, nó hoạt động tốt:

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

Trong jQuery bạn có thể đặt mã trong vòng phương pháp .ready() để thực thi mã của bạn đầu tiên khi DOM được nạp đầy đủ:

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

Hãy thử bọc nó vào tài liệu r eady chức năng và chắc chắn, rằng bạn có jquery bao gồm.

<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(); 
     } 

+0

Thật tuyệt khi có một số mã, nhưng sẽ rất tuyệt nếu bạn có thể thêm một chút giải thích. –

+0

Tôi không chắc chắn cách tìm câu trả lời cho câu hỏi trong mã này. –

2

Tôi cũng đã phải đối mặt với cùng một problem.To giải quyết vấn đề này, đặt mã của bạn trong chức năng setTimeout.

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

Làm điều này với thời gian chờ chỉ là nhanh chóng và bẩn – Unknown

+0

Có, việc thêm thời gian chờ chỉ là một giải pháp vì chúng tôi cần loại đánh dấu tiếp theo của sự kiện để vẽ lại DOM để hiển thị các thay đổi. Chúng tôi có thể chọn cách tiếp cận khác để giải quyết vấn đề này :-) –

+0

Tôi đã giải quyết nó bằng cách áp dụng trình nghe "animationend" cho hoạt ảnh của đối tượng đã tạo (vì đối tượng của tôi xuất hiện với hoạt ảnh). Bằng cách này nó được tập trung một khi nó là nghĩa vụ phải được. – Unknown

Các vấn đề liên quan