替换/重建输入元素和更新PrototypeJS焦点/模糊函数

Replace/Recreate Input Element and Update PrototypeJS Focus/Blur Functions

本文关键字:PrototypeJS 焦点 函数 更新 模糊 重建 输入 元素 替换      更新时间:2023-09-26

我有模糊和焦点上的功能,更改密码文本字段从文本更改为密码类型。发生的情况是,我们删除并重新创建它,但原型函数之后不能工作。代码如下:

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;
        }
    }
}

它没有修复IE7和IE8的问题,但至少它可以在所有其他主流浏览器上工作,因为它们支持更改输入类型。