• <bdo id='0BEFR'></bdo><ul id='0BEFR'></ul>

      1. <i id='0BEFR'><tr id='0BEFR'><dt id='0BEFR'><q id='0BEFR'><span id='0BEFR'><b id='0BEFR'><form id='0BEFR'><ins id='0BEFR'></ins><ul id='0BEFR'></ul><sub id='0BEFR'></sub></form><legend id='0BEFR'></legend><bdo id='0BEFR'><pre id='0BEFR'><center id='0BEFR'></center></pre></bdo></b><th id='0BEFR'></th></span></q></dt></tr></i><div id='0BEFR'><tfoot id='0BEFR'></tfoot><dl id='0BEFR'><fieldset id='0BEFR'></fieldset></dl></div>
        <legend id='0BEFR'><style id='0BEFR'><dir id='0BEFR'><q id='0BEFR'></q></dir></style></legend>
        <tfoot id='0BEFR'></tfoot>

        <small id='0BEFR'></small><noframes id='0BEFR'>

      2. 仅使用 Javascript 显示/隐藏单击按钮的密码

        Show/hide password onClick of button using Javascript only(仅使用 Javascript 显示/隐藏单击按钮的密码)
          <tbody id='tJqKQ'></tbody>
        <tfoot id='tJqKQ'></tfoot>

        <small id='tJqKQ'></small><noframes id='tJqKQ'>

                <i id='tJqKQ'><tr id='tJqKQ'><dt id='tJqKQ'><q id='tJqKQ'><span id='tJqKQ'><b id='tJqKQ'><form id='tJqKQ'><ins id='tJqKQ'></ins><ul id='tJqKQ'></ul><sub id='tJqKQ'></sub></form><legend id='tJqKQ'></legend><bdo id='tJqKQ'><pre id='tJqKQ'><center id='tJqKQ'></center></pre></bdo></b><th id='tJqKQ'></th></span></q></dt></tr></i><div id='tJqKQ'><tfoot id='tJqKQ'></tfoot><dl id='tJqKQ'><fieldset id='tJqKQ'></fieldset></dl></div>

                • <bdo id='tJqKQ'></bdo><ul id='tJqKQ'></ul>

                • <legend id='tJqKQ'><style id='tJqKQ'><dir id='tJqKQ'><q id='tJqKQ'></q></dir></style></legend>
                  本文介绍了仅使用 Javascript 显示/隐藏单击按钮的密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想在仅使用 Javascript 单击眼睛图标时创建密码切换功能.我已经为它编写了代码,但它仅用于显示密码文本而不是相反.谁能看到下面代码中的逻辑错误.

                  I want to create password toggle function when clicked on the eye icon using Javascript only. I have written code for it but it works only to show the password text and not the other way round. Can someone see the logic error in the code below.

                  function show() {
                    var p = document.getElementById('pwd');
                    p.setAttribute('type', 'text');
                  }
                  
                  function hide() {
                    var p = document.getElementById('pwd');
                    p.setAttribute('type', 'password');
                  }
                  
                  function showHide() {
                    var pwShown = 0;
                  
                    document.getElementById("eye").addEventListener("click", function() {
                      if (pwShown == 0) {
                        pwShown = 1;
                        show();
                      } else {
                        pwShow = 0;
                        hide();
                      }
                    }, false);
                  }

                  <input type="password" placeholder="Password" id="pwd" class="masked" name="password" />
                  <button type="button" onclick="showHide()" id="eye">
                    <img src="eye.png" alt="eye"/>
                  </button>

                  推荐答案

                  每次单击按钮时,您都会绑定单击事件.您不想要多个事件处理程序.另外,您在每次点击时重新定义 var pwShown = 0,因此您永远无法恢复输入状态(pwShown 保持不变).

                  You are binding click event every time you click a button. You don't want multiple event handlers. Plus you are redefining var pwShown = 0 on every click so you can never revert input state (pwShown stays the same).

                  移除 onclick 属性并与 addEventListener 绑定点击事件:

                  Remove onclick attribute and bind click event with addEventListener:

                  function show() {
                      var p = document.getElementById('pwd');
                      p.setAttribute('type', 'text');
                  }
                  
                  function hide() {
                      var p = document.getElementById('pwd');
                      p.setAttribute('type', 'password');
                  }
                  
                  var pwShown = 0;
                  
                  document.getElementById("eye").addEventListener("click", function () {
                      if (pwShown == 0) {
                          pwShown = 1;
                          show();
                      } else {
                          pwShown = 0;
                          hide();
                      }
                  }, false);

                  <input type="password" placeholder="Password" id="pwd" class="masked" name="password" />
                  <button type="button" id="eye">
                      <img src="https://cdn0.iconfinder.com/data/icons/feather/96/eye-16.png" alt="eye" />
                  </button>

                  这篇关于仅使用 Javascript 显示/隐藏单击按钮的密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
                  问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  Ordinals in words javascript(javascript中的序数)

                    <small id='ZEaXy'></small><noframes id='ZEaXy'>

                      • <bdo id='ZEaXy'></bdo><ul id='ZEaXy'></ul>
                      • <i id='ZEaXy'><tr id='ZEaXy'><dt id='ZEaXy'><q id='ZEaXy'><span id='ZEaXy'><b id='ZEaXy'><form id='ZEaXy'><ins id='ZEaXy'></ins><ul id='ZEaXy'></ul><sub id='ZEaXy'></sub></form><legend id='ZEaXy'></legend><bdo id='ZEaXy'><pre id='ZEaXy'><center id='ZEaXy'></center></pre></bdo></b><th id='ZEaXy'></th></span></q></dt></tr></i><div id='ZEaXy'><tfoot id='ZEaXy'></tfoot><dl id='ZEaXy'><fieldset id='ZEaXy'></fieldset></dl></div>
                          1. <tfoot id='ZEaXy'></tfoot>
                            <legend id='ZEaXy'><style id='ZEaXy'><dir id='ZEaXy'><q id='ZEaXy'></q></dir></style></legend>
                              <tbody id='ZEaXy'></tbody>