当我分离当前的焦点元素并再次插入时,我遇到使用Tab键移动字段的问题。请在jsfiddle.中找到我的代码。另见下文:
JS代码:
$(document).ready(function() {
$('.formElem').focusin(function() {
// Remove default text on focus in
if ($(this).val() == $(this).attr('title')) {
$(this).val('').removeClass('defaultText');
if (($(this).attr('id') == 'reg_pwd') || ($(this).attr('id') == 'reg_conf_pwd')) {
id = '#' + $(this).attr('id');
marker = $('<span>123</span>').insertBefore($(this));
$(this).detach().attr('type', 'password').insertAfter(marker);
marker.remove();
}
if ($(this).get(0) != $(':focus').get(0)) {
$(this).focus();
}
}
}).focusout(function() {
// Remove default text on focus out
if ($(this).val() === '') {
$(this).val($(this).attr('title')).addClass('defaultText');
if (($(this).attr('id') == 'reg_pwd') || ($(this).attr('id') == 'reg_conf_pwd')) {
marker = $('<span>123</span>').insertBefore($(this));
$(this).detach().attr('type', 'text').insertAfter(marker);
marker.remove();
}
}
});
});
代码将字段的类型从文本更改为密码和来回。当您单击或使用Tab键到达密码字段时,它会在再次添加后失去焦点。谢谢,如果有人能弄清楚原因。
答案 0 :(得分:0)
看来你使用的是html5,所以也许你可以利用placeholder
属性。而不是<input value="blah" />
使用<input placeholder="blah" />
。现在你甚至不需要拥有defaultText类,因此可以删除它,以及删除和添加类的相关jquery代码。
现在,您还可以将<input type="text" />
更改为<input type="password" />
以获取密码字段。我不确定你尝试用<span>123</span>
做什么。或许,我不完全理解你的问题。但是下面似乎可以达到你想要的效果。
jsfiddle以及http://jsfiddle.net/8BBRC/2
编辑:错误的jsfiddle链接。
<input type="text" class="formElem" id="reg_fullname" name="fullname" placeholder="Full Name" title="Full Name" /><br />
<input type="email" class="formElem" id="reg_email" name="email" placeholder="Email Address" title="Email Address" /><br />
<input type="password" class="formElem" id="reg_pwd" name="pwd" placeholder="Confirm" title="Password" /><br />
<input type="password" class="formElem" id="reg_conf_pwd" name="conf_pwd" placeholder="Confirm Password" title="Confirm Password" /><br />
<input type="submit" id="reg_submit" value="Submit" /><br />