2016-09-28 47 views
0

點擊登錄按鈕,但我收到警報消息,但在關閉警報時,下一個焦點將返回到第1個文本框的用戶名。我希望焦點回到按鈕上,以便我可以選擇下一個字段。保留重點元素

<html> 
 
    <head> 
 
    <title> Test </title> 
 
    <script> 
 
     function handleClick(){ 
 
     alert("welcome"); 
 
     } 
 
    </script> 
 
    </head> 
 

 
    <body> 
 
    <form name="aye" onSubmit="handleClick();"> 
 
     <label for="username">Username:</label> 
 
     <input type="text" id="username" tabindex="0"> 
 
     <br> 
 
     <input type="submit" value="Log in" tabindex="0"> 
 
     <br> 
 
     <label for="password">Password:</label> 
 
     <input type="password" id="password" tabindex="0"> 
 
     <br> 
 
    </form> 
 
    </body>  
 
</html>

回答

1

如果我理解正確的話,你可以用做一個元件上的.focus()

document.querySelector('yourselector').focus(); 

像這樣:

<html> 
 
    <head> 
 
    <title> Test </title> 
 
    <script> 
 
     function handleClick(){ 
 
     alert("welcome"); 
 
     document.querySelector('input[type="submit"]').focus(); 
 
     } 
 
    </script> 
 
    </head> 
 

 
    <body> 
 
    <form name="aye" onSubmit="handleClick();"> 
 
     <label for="username">Username:</label> 
 
     <input type="text" id="username" tabindex="0"> 
 
     <br> 
 
     <input type="submit" value="Log in" tabindex="0"> 
 
     <br> 
 
     <label for="password">Password:</label> 
 
     <input type="password" id="password" tabindex="0"> 
 
     <br> 
 
    </form> 
 
    </body>  
 
</html>

+0

關閉警告框後,我希望將焦點重新放回「登錄」按鈕本身。我沒有得到它。 – Guru

+0

以上的任何更新? – Guru

+0

儘量將焦點放在按鈕上。將選擇器更改爲'input [type =「submit」]' –

0

使用代碼

$(document).ready(function() { 
     $('input[type=submit]').focus(); 
    }); 

將焦點設置提交按鈕。