$('#lPass').focus(function() {
if (this.value == this.defaultValue) this.value = '';
$(this).after('<input type="password" id="lPass" size="10" value="'+this.value+'"/>').remove();
}).blur(function() {
alert(1);
});
<input id="lPass" type="text" size="10" value="Password"/>
Run Code Online (Sandbox Code Playgroud)
onblur不工作.
有任何想法吗?
Amr*_*rhy 26
使用live而不是focus和blur
因为你是在运行时添加输入,所以它添加到没有事件的页面,live:
现在或将来,为当前选择器匹配的所有元素附加处理程序.
例:
$('#lPass').on('focus', function() {
if (this.value == this.defaultValue) this.value = '';
$(this).after('<input type="password" id="lPass" size="10" value="' + this.value + '"/>').remove();
});
$('#lPass').on('blur', function() {
alert(1);
});
Run Code Online (Sandbox Code Playgroud)