代码之家  ›  专栏  ›  技术社区  ›  Roshana Pitigala Laxmansinghsodhanohdiyala

在键向下/键向上切换显示/隐藏密码

  •  1
  • Roshana Pitigala Laxmansinghsodhanohdiyala  · 技术社区  · 6 年前

    当且仅当用户 点击并保持 按钮。这意味着当用户释放点击时,应该再次隐藏它。我试过了 onkeyup onkeydown 但到目前为止我什么都没有。

    function show() {
      $(pass).attr("type", "text");
    }
    
    function hide() {
      $(pass).attr("type", "password");
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <input type='password' id='pass' value='abcdef' />
    <button onkeydown='show()' onkeyup='hide()'>toggle</button>

    我这里缺什么?

    2 回复  |  直到 6 年前
        1
  •  2
  •   Luca Kiebel    6 年前

    mousedown mouseup

    function show() {
      $(pass).attr("type", "text");
    }
    
    function hide() {
      $(pass).attr("type", "password");
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <input type='password' id='pass' value='abcdef' />
    <button onmousedown='show()' onmouseup='hide()'>toggle</button>
        2
  •  1
  •   Vignesh Raja    6 年前

    onmousedown onmouseup ontouchstart ontouchend

    function show() {
      document.getElementById("pass").setAttribute("type","text");
    }
    
    function hide() {
      document.getElementById("pass").setAttribute("type","password");
    }
    <input type='password' id='pass' value='abcdef' />
    <button onmousedown='show()' onmouseup='hide()'>toggle</button>