• <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 显示/隐藏单击按钮的密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  keeping radio buttons checked after form submit(表单提交后保持选中单选按钮)
                  Bootstrap radio button quot;checkedquot; flag(Bootstrap 单选按钮“已选中旗帜)
                  Toggle HTML radio button by clicking its label(通过单击标签来切换 HTML 单选按钮)
                  Javascript how to change radio button label text?(Javascript如何更改单选按钮标签文本?)
                  JavaScript radio button confirmation(JavaScript 单选按钮确认)
                  How can I automatically select specific radio buttons with Greasemonkey?(如何使用 Greasemonkey 自动选择特定的单选按钮?)

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