Mis*_*hko 7 html javascript forms jquery focus
我有一个简单的登录表单,其中包含2个输入字段:"username"和"password"."用户名"字段默认为焦点.问题是,当用户点击"用户名"或"密码"字段外,焦点消失了(既不是"用户名"也不是"密码"字段").如何强调焦点在这两个字段上?只要 ?
在我的情况下,这是一个非常讨厌的行为,所以我真的想这样做:)
我可以这样做:
$("*").focus(function() {
if (!$(this).hasClass("my_inputs_class")) {
// How to stop the focusing process here ?
}
});
Run Code Online (Sandbox Code Playgroud)
?
听起来你总是希望你的一个投入能够集中,公平.我这样做的方法是绑定每个输入blur()事件,这样如果它发生,它将转到下一个元素.
这是标记:
<body>
<form method="POST" action=".">
<input type="text" name="username" />
<input type="password" name="password" />
<input type="submit" name="submit" />
</form>
</body>
Run Code Online (Sandbox Code Playgroud)
这是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();
});
Run Code Online (Sandbox Code Playgroud)
你可以使用javascript设置焦点焦点,但你真的不应该.强制关注这些字段会破坏页面的正常交互.这意味着用户无法像点击页面上的链接那样简单,因为焦点总是在那些输入上.
请不要这样做:)