javascript - 如何设置点击图标时可见的密码

标签 javascript html css font-awesome-5

我有三个输入字段,它们都是密码字段,我想做的是在输入字段中放置 fontawesome 图标,点击它可以显示或隐藏密码

目前我有这个但是有按钮,它只对一个字段而不是所有三个字段执行,我必须为所有三个调用 3 个不同的函数还是可以用相同的函数完成

片段

function show() {
  var a = document.getElementById("confirm_password");
  var b = document.getElementById("display2");
  if (a.type == "password") {
    a.type = "text";

  } else {
    a.type = "password";

  }
}
<div class="col-lg-3">
  <h5 id="commonHeader">Current Password</h5>
  <input type="password" id="currentPass" name="currentpass" class="commanClass" placeholder="Current Password">
  <button type="button" onclick="show()" id="display"></button>
</div>
<div class="col-lg-3">
  <h5 id="commonHeader">New Password</h5>
  <input type="password" id="newPass" name="newpass" required="required" class="commanClass" placeholder="Current Password">
  <button type="button" onclick="show()" id="display1"></button>

</div>
<div class="col-lg-3">
  <h5 id="commonHeader">Confirm Password</h5>
  <input type="password" id="confirm_password" required="required" placeholder="Confirm Password">
  <button type="button" onclick="show()" id="display2"></button>
</div>

<div>
  <hr>
  <br>
  <button type="submit" id="save" class="commonButton">Save</button>
  <button type="button" id="save" class="commonButton">Clear</button>
</div>

在我的代码片段中,我有 3 个输入字段,我必须为所有输入字段提供一个图标,如果用户单击该图标,它将显示当前字段的密码

目前只适用于一个领域

最佳答案

您可以使用 removeAttribute 删除 type="password"并同时使用 setAttribute 将类型设置为文本

function show(a) {
  var x=document.getElementById(a);
  var c=x.nextElementSibling
  if (x.getAttribute('type') == "password") {
  c.removeAttribute("class");
  c.setAttribute("class","fas fa-eye");
  x.removeAttribute("type");
    x.setAttribute("type","text");
  } else {
  x.removeAttribute("type");
    x.setAttribute('type','password');
 c.removeAttribute("class");
  c.setAttribute("class","fas fa-eye-slash");
  }
}
  
   
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.1/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
<div class="col-lg-3">
  <h5 id="commonHeader">Current Password</h5>
  <input type="password" id="currentPass" name="currentpass" class="commanClass" placeholder="Current Password">
  <i onclick="show('currentPass')" class="fas fa-eye-slash" id="display"></i>
</div>
<div class="col-lg-3">
  <h5 id="commonHeader">New Password</h5>
  <input type="password" id="newPass" name="newpass" required="required" class="commanClass" placeholder="Current Password">
 <i onclick="show('newPass')" class="fas fa-eye-slash" id="display"></i>

</div>
<div class="col-lg-3">
  <h5 id="commonHeader">Confirm Password</h5>
  <input type="password" id="confirm_password" required="required" placeholder="Confirm Password">
 <i onclick="show('confirm_password')" class="fas fa-eye-slash" id="display"></i>
</div>

<div>
  <hr>
  <br>
  <button type="submit" id="save" class="commonButton">Save</button>
  <button type="button" id="save" class="commonButton">Clear</button>
</div>

关于javascript - 如何设置点击图标时可见的密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54528416/

相关文章:

javascript - 什么是exports.displayName =(未定义: ?字符串);意味着 react native ?

javascript - Google Charts DataView 工具提示 HTML 呈现为字符串

javascript - jQuery/在另一个元素内附加一个 div 元素并定位/控制父元素

html - 更改此 img 正方形并在卡内响应

javascript - 避免 jQuery 中的巨大级联?

javascript - 如何使用 CSS 或 Javascript 定位 Mac

javascript - 使用 ui-router 重定向到另一个页面的提交按钮

javascript - 叠加在图像上的谷歌地图会使图像消失

javascript - 移动滑动菜单可滚动

html - Actionmailer css 添加到整个应用程序