如何使用javascript将焦点设置为文本框

How to set the focus to a text-box using javascript

本文关键字:文本 设置 焦点 何使用 javascript      更新时间:2024-04-25

我正试图使用JavaScript将焦点设置在文本框上,但当我运行时,它不起作用——换句话说,它没有将焦点设置到文本框上。我在这里做错了什么?

     <script type="text/javascript">
         function validate() {
             var sleepQuality = document.getElementById('<%=txtRate.ClientID %>').value;
             if (sleepQuality == "") {
                 alert("Please enter something....... ");
                 return false;
                 document.getElementById('<%=txtRate.ClientID %>').focus();
             }
         }
</script>

Return退出函数。执行后什么都没有。

alert("Please enter something....... ");
return false;
document.getElementById('<%=txtRate.ClientID %>').focus();  //<-- I will not run

在之前移动

alert("Please enter something....... ");
document.getElementById('<%=txtRate.ClientID %>').focus();  //<-- I will run
return false;