我試圖將焦點設置爲一個元素,然後立即將其設置回以前的焦點項目。這裏的HTML:jQuery專注於一個元素,然後另一個立即
<input id="em3" type="email" />
<p class="error" tabindex="0">Please fill out the email field</p>
我已經試過這樣:
$(this).next('.error').focus();
$(this).prev('input').focus();
這:
$(this).next('.error').focus();
setTimeout(function(){
$(this).prev('input').focus();
}, 2000);
但無論是作品。焦點停留在「.error」元素上。
任何想法?
運行代碼時,哪個元素是'this'?改變焦點到下一個元素不會改變'this'。所以,如果你從'input'開始並想回到它,它應該是'$(this).focus()'。 – Barmar