【问题标题】:Eye in password field does not appear using bootstrap 5使用引导程序 5 不会出现密码字段中的眼睛
【发布时间】:2021-08-31 22:29:03
【问题描述】:

当使用 Bootstrap v5 和 FontAwesome v5 时,我无法让眼球出现在表单的右侧。相反,它会将密码字段拉到比用户名字段更靠右的位置,并且眼睛不会显示。

<div class="input-group mb-3">
   <span class="input-group-text"><i class="fas fa-lock"></i></span>
   <input class="form-control" id="username" name="username" placeholder="Username" value="">
</div>

<div class="input-group mb-3">
   <span class="input-group-text"><i class="fas fa-lock"></i></span>
   <input class="form-control" id="password" name="password" placeholder="Password" value="">
   <span class="input-group-text"><i class="far fa-eye-slash" id="togglePassword"></i></span>
</div>

Javascript

const togglePassword = document.querySelector("#togglePassword");
const password = document.querySelector("#password");

togglePassword.addEventListener("click", function () {
   
// toggle the type attribute
const type = password.getAttribute("type") === "password" ? "text" : "password";
password.setAttribute("type", type);

// toggle the eye icon
this.classList.toggle('fa-eye');
});

【问题讨论】:

  • 你有没有加载字体真棒字体/css?其他图标在您的网站中是否有效(例如,这是唯一没有显示的图标)?
  • 是的,我的图标有效。当我删除左边距时,它会出现,但在输入字段之外。

标签: html css bootstrap-5 font-awesome-5


【解决方案1】:

当图标与输入重叠时,它位于输入标记下方。要解决此问题,您需要将&lt;i&gt; 标记设置为更高的z-index 值。尝试使用z-index: 100 使其即使在单击输入时也显示在顶部。

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.4/css/fontawesome.min.css" integrity="sha384-jLKHWM3JRmfMU0A5x5AkjWkw/EYfGUAGagvnfryNV3F9VqM98XiIH7VBGVoxVSc7" crossorigin="anonymous">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We" crossorigin="anonymous">
<div class="input-group mb-3">
   <span class="input-group-text"><i class="fas fa-lock"></i></span>
   <input class="form-control" id="password" name="password" placeholder="Password" value="">
   <i class="far fa-eye" id="togglePassword" 
   style="cursor: pointer; margin-left: -30px; z-index: 100;"></i>
</div>

编辑:如果您希望密码字段和用户名字段的宽度相同,您可以尝试将图标包裹在input-group-text 内并删除margin-left: -30px; z-index: 100; 的样式,因为@987654328 @ 与输入字段重叠。

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.4/css/fontawesome.min.css" integrity="sha384-jLKHWM3JRmfMU0A5x5AkjWkw/EYfGUAGagvnfryNV3F9VqM98XiIH7VBGVoxVSc7" crossorigin="anonymous">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We" crossorigin="anonymous">

<div class="input-group mb-3">
  <span class="input-group-text"><i class="fas fa-lock"></i></span>
  <input class="form-control" id="username" name="username" placeholder="Username" value="">
</div>

<div class="input-group mb-3">
  <span class="input-group-text"><i class="fas fa-lock"></i></span>
  <input class="form-control" id="password" name="password" placeholder="Password" value="">
  <span class="input-group-text">
    <i class="far fa-eye" id="togglePassword" 
   style="cursor: pointer"></i>
   </span>
</div>

编辑 2: 在您的 JavaScript 代码中,您使用 this.classList.toggle('fa-eye') 隐藏了眼睛图标。该函数在togglePassword元素的classList中可用时移除类fa-eye,当不可用时添加上述类。

我假设您想在用户当前使用文本格式时显示隐藏密码图标,这些图标可能是 eye-slash icon。所以你可以添加

this.classList.toggle('fa-eye-slash');

如下

const togglePassword = document.querySelector("#togglePassword");
const password = document.querySelector("#password");

togglePassword.addEventListener("click", function () {
   
  // toggle the type attribute
  const type = password.getAttribute("type") === "password" ? "text" : "password";
  password.setAttribute("type", type);
  // toggle the eye icon
  this.classList.toggle('fa-eye');
  this.classList.toggle('fa-eye-slash');
});
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.4/css/fontawesome.min.css" integrity="sha384-jLKHWM3JRmfMU0A5x5AkjWkw/EYfGUAGagvnfryNV3F9VqM98XiIH7VBGVoxVSc7" crossorigin="anonymous">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We" crossorigin="anonymous">

<div class="input-group mb-3">
  <span class="input-group-text"><i class="fas fa-lock"></i></span>
  <input class="form-control" id="username" name="username" placeholder="Username" value="">
</div>

<div class="input-group mb-3">
  <span class="input-group-text"><i class="fas fa-lock"></i></span>
  <input class="form-control" id="password" name="password" placeholder="Password" value="">
  <span class="input-group-text">
    <i class="far fa-eye" id="togglePassword" 
   style="cursor: pointer"></i>
   </span>
</div>

【讨论】:

  • 这部分工作 - 我现在看到了眼睛,但是,密码字段仍然延伸到右侧,越过它上面的用户名字段。
  • @germanshedder 我已经修改了解决方案
  • 我做到了,现在字段确实匹配宽度。但是,当我单击眼睛时,它会消失。我在原始帖子中添加了我的 javascript 代码。
  • 添加了更多解释和善意提醒,在您以后的帖子中,让您的问题一开始就集中在一个特定问题上,并记住 Stack Overflow 不仅仅是一个提供调试建议的站点
  • 它在 Chrome 中有效,但是在 Edge 中,我看到眼睛也出现在输入字段中。
【解决方案2】:

尝试将z-index:2; 添加到您的图标中。 为防止输入字段聚焦时眼睛图标被隐藏,您需要添加以下 css 代码:-

<style>
#password:focus ~ .far-fa-eye{
margin-left: -30px !important; 
cursor: pointer !important;
z-index: 5 !important;
}
</style>

另外,请与我们分享您的用户名字段的视觉效果或代码,以便我们更好地了解您的意思。

PS:我希望尽可能避免使用内联 css。在调试大型 css 代码时,内联 css 可能会很麻烦。

【讨论】:

    【解决方案3】:

    使用 Bootstrap 5 实现 Jquery 3.6.0

    HTML

    <div class="mb-3">
        <label class="form-label">Password</label>
        <div class="input-group mb-3">
            <input class="form-control password" id="password" class="block mt-1 w-full" type="password" name="password" required />
            <span class="input-group-text">
                <i class="far fa-eye-slash" id="togglePassword"
                style="cursor: pointer"></i>
            </span>
        </div>
    </div>
    

    JS

    $(".togglePassword").click(function (e) {
    e.preventDefault();
    var type = $(this).parent().parent().find(".password").attr("type");
    console.log(type);
    if(type == "password"){
        $(this).removeClass("fa-eye-slash");
        $(this).addClass("fa-eye");
        $(this).parent().parent().find(".password").attr("type","text");
    }else if(type == "text"){
        $(this).removeClass("fa-eye");
        $(this).addClass("fa-eye-slash");
        $(this).parent().parent().find(".password").attr("type","password");
    }});
    

    【讨论】:

      猜你喜欢
      • 2013-09-28
      • 2021-07-06
      • 2020-12-11
      • 2021-11-10
      • 1970-01-01
      • 2017-10-10
      • 1970-01-01
      • 2014-04-02
      • 1970-01-01
      相关资源
      最近更新 更多