Javascript 替换/重新创建输入元素并更新PrototypeJS焦点/模糊函数
我有模糊和聚焦功能,可以将密码文本字段从文本更改为密码类型。结果是我们删除并重新创建了它,但是原型函数之后就不起作用了。代码如下:Javascript 替换/重新创建输入元素并更新PrototypeJS焦点/模糊函数,javascript,prototypejs,Javascript,Prototypejs,我有模糊和聚焦功能,可以将密码文本字段从文本更改为密码类型。结果是我们删除并重新创建了它,但是原型函数之后就不起作用了。代码如下: function focusPass(inputel,inputval) { if(inputel.value == inputval) { var newInput = document.createElement('input'); newInput.setAttribute('type','password');
function focusPass(inputel,inputval) {
if(inputel.value == inputval) {
var newInput = document.createElement('input');
newInput.setAttribute('type','password');
newInput.setAttribute('name',inputel.getAttribute('name'));
newInput.setAttribute('id',inputel.getAttribute('id'));
inputel.parentNode.replaceChild(newInput,inputel);
setTimeout(function() { newInput.focus(); }, 10);
}
}
function blurPass(inputel,inputval) {
if(inputel.value == '') {
var newInput = document.createElement('input');
newInput.setAttribute('type','text');
newInput.setAttribute('name',inputel.getAttribute('name'));
newInput.setAttribute('id',inputel.getAttribute('id'));
newInput.value = inputval;
inputel.parentNode.replaceChild(newInput,inputel);
}
}
if ($('j_password') != undefined) {
blurPass($('j_password'),'password');
$('j_password').observe('blur', function(e) {
blurPass(this,'password');
});
$('j_password').observe('focus', function(e) {
focusPass(this,'password');
});
}
我试过这样做:
document.observe('blur', function(e, el) {
if (el = e.findElement('#j_password')) {
blurPass(this,'password');
}
});
document.observe('focus', function(e, el) {
if (el = e.findElement('#j_password')) {
focusPass(this,'password');
}
});
但这似乎没有任何作用。有人能告诉我如何保持这个功能工作吗?我不希望必须为onblur和onfocus设置属性,但如果不可能,将恢复到该属性
谢谢 我最后做的是:
function focusPass(inputel,inputval) {
if(inputel.value == inputval) {
var ie = getIEVersion();
if (ie > -1 && ie < 9) {
var newInput = document.createElement('input');
newInput.setAttribute('type','password');
newInput.setAttribute('name',inputel.getAttribute('name'));
newInput.setAttribute('id',inputel.getAttribute('id'));
new Insertion.After(inputel, newInput);
inputel.remove();
setTimeout(function() { newInput.focus(); }, 10);
} else {
inputel.setAttribute('type','password');
inputel.value = "";
inputel.focus();
}
}
}
function blurPass(inputel,inputval) {
if(inputel.value == '') {
var ie = getIEVersion();
if (ie > -1 && ie < 9) {
var newInput = document.createElement('input');
newInput.setAttribute('type','text');
newInput.setAttribute('name',inputel.getAttribute('name'));
newInput.setAttribute('id',inputel.getAttribute('id'));
newInput.value = inputval;
new Insertion.After(inputel, newInput);
inputel.remove();
} else {
inputel.setAttribute('type','text');
inputel.value = inputval;
}
}
}
函数焦点类(inputel,inputval){
if(inputel.value==inputval){
var ie=getIEVersion();
如果(ie>-1&&ie<9){
var newInput=document.createElement('input');
setAttribute('type','password');
newInput.setAttribute('name',inputel.getAttribute('name');
newInput.setAttribute('id',inputel.getAttribute('id');
新插入。之后(inputel,newInput);
inputel.remove();
setTimeout(函数(){newInput.focus();},10);
}否则{
setAttribute('type','password');
inputel.value=“”;
inputel.focus();
}
}
}
函数传递(inputel,inputval){
如果(inputel.value==''){
var ie=getIEVersion();
如果(ie>-1&&ie<9){
var newInput=document.createElement('input');
setAttribute('type','text');
newInput.setAttribute('name',inputel.getAttribute('name');
newInput.setAttribute('id',inputel.getAttribute('id');
newInput.value=inputval;
新插入。之后(inputel,newInput);
inputel.remove();
}否则{
setAttribute('type','text');
inputel.value=inputval;
}
}
}
它不能解决IE7和IE8的问题,但至少它可以在所有其他主流浏览器上使用,因为它们支持更改输入类型