聽起來像你總是希望你的輸入之一是專注,公平的。我會這樣做的方式是綁定每個輸入事件,以便發生時它將轉到下一個元素。
這裏的標記:
<body>
<form method="POST" action=".">
<input type="text" name="username" />
<input type="password" name="password" />
<input type="submit" name="submit" />
</form>
</body>
而這裏的jQuery的:
$(document).ready(function() {
// what are the named fields the user may focus on?
var allowed = ['username', 'password', 'submit'];
$.each(allowed, function(i, val) {
var next = (i + 1) % allowed.length;
$('input[name='+val+']').bind('blur', function(){
$('input[name='+allowed[next]+']').focus();
});
});
$('input[name='+allowed[0]+']').focus();
});
Eugh。請不要嘗試這樣做。如果用戶想把焦點放在別的地方,讓他們把它放在別的地方。 – Quentin 2010-06-10 14:14:34
也許這種非常惱人的焦點行爲是你被要求做的事情,所以你沒有什麼權力去做別的事情,但是想一想:是否真的值得麻煩?它有用嗎?甚至有必要? – Humberto 2010-06-10 14:23:32
@Misha:歡迎編程一個Web應用程序! – 2010-06-13 17:15:30