[英]jquery: focus/blur events don't work after input replacement
I have a simple input element with the following attributes: 我有一个具有以下属性的简单输入元素:
<input id="InputPassword" type="password" value="Password" class="initClass"/>
In order set a cross-browser placeholder for it I use testFocusBlur function 为了为其设置跨浏览器占位符,我使用了testFocusBlur函数
function testFocusBlur(){
$('input').focus(function () {
if($(this).attr('type')=='text'){
var campoPassword=$(this);
replaceInputType(campoPassword[0], 'password')
}
}).blur(function () {
if($(this).attr('type')=='password'){
var campoPassword=$(this);
replaceInputType(campoPassword[0], 'text')
}
});
$('input').blur();
} }
which, on $(document).ready event: 在$(document).ready事件中:
the above works correctly but, once the new input element is set, and I focus on it, the initial testFocusBlur function doesn't work anymore 上面的方法可以正常工作,但是,一旦设置了新的输入元素,并且我专注于此,初始的testFocusBlur函数就不再起作用
see: http://jsfiddle.net/4tRzs/2/ 参见: http : //jsfiddle.net/4tRzs/2/
note: i don't want to set an inline function in the input such as onfocus="...." 注意:我不想在输入中设置内联函数,例如onfocus =“ ....”
Please help, Thanks. 请帮忙,谢谢。
You need to use on
function from jQuery 您需要使用jQuery中的on
函数
$(document).on("focus" , "input", function(){
...
}
on
is the old live
function. on
是旧的live
功能。 documentation 文件资料
You will have something like this in your function: 您的函数中将具有以下内容:
function testFocusBlur(){
$(document).on("focus", 'input', function () {
if($(this).attr('type')=='text'){
var campoPassword=$(this);
replaceInputType(campoPassword[0], 'password')
}
})
$(document).on("blur", "input", function () {
if($(this).attr('type')=='password'){
var campoPassword=$(this);
replaceInputType(campoPassword[0], 'text')
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.