在javascript中切换标签元素 [英] Toggle label element in javascript

查看:77
本文介绍了在javascript中切换标签元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我最近问过
这是图像,所以我尝试了一些新的东西。

  function togglePassword(obj){
var el = document.getElementById(obj);
var input_el = document.getElementById('password_field');
if(input_el.value.length> 0)
{
el.style.display ='none';
}
else {
if(el.style.display!='none'){
el.style.display ='none';
}
else {
el.style.display ='';
}
}
}

不幸的是,此代码没有'工作..有人能指出我在哪里弄错了吗?因此,第二个代码应该检查密码字段是否为emtpy,如果它不为空,则继续toogle如果不切换只是隐藏密码值。帮助:D谢谢

解决方案

试试这个它会起作用

  function togglePassword(obj){
var el = document.getElementById(obj);
var input_el = document.getElementById('password_field');
if((input_el.style.display =='none')||((input_el.style.display =='')&&(input_el.offsetWidth == 0))){
el。 style.display ='block';
} else {
el.style.display ='none';
}
}
}


Hello everybody I've recently asked this question and I've given up with the 'replace input idea'. Instead I've decided to try another approach..

I've made my password field background transparent and I placed <label> element "behind" the password input, so that it appears to be part of the password field. Now I wrote a standard function for toggling element like this :

function togglePassword(obj) {
                    var el = document.getElementById(obj);
                    if ( el.style.display != 'none' ) {
                        el.style.display = 'none';
                    }
                    else {
                        el.style.display = '';
                    }
                }

I trigger this function onforus and onblur from password field so that label disappears when the password field is focused but I have problems with onblur, because after I type password and hit TAB on the which takes me to login submit button then the <label> element appears with password value again and its mixed with typed password. Here is the image of that, so I tried to come up with something new.

function togglePassword(obj) {
                var el = document.getElementById(obj);
                        var input_el = document.getElementById('password_field');
                   if(input_el.value.length > 0)
                            {
                                el.style.display = 'none';
                            }
                           else {
                              if ( el.style.display != 'none' ) {
                        el.style.display = 'none';
                        }
                        else {
                            el.style.display = '';
                        }
                    }
                               }

Unfortunately, this code doesn't work .. can someone point to me where did I made mistake? So this second code should check whether password field is emtpy or not, if its not empty then continue to toogle if it is don't toggle just hide the password value. Help :D thank you

解决方案

Try this it will work

function togglePassword(obj) { 
var el = document.getElementById(obj); 
var input_el = document.getElementById('password_field'); 
if((input_el.style.display == ‘none’) || ((input_el.style.display=='') && (input_el.offsetWidth==0))){
el.style.display = 'block';
} else {
el.style.display = ‘none’;
}
}
                               } 

这篇关于在javascript中切换标签元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆