html - 悬停自动完成建议使元素消失

标签 html css hover

我有一个登录表单,当您将鼠标悬停在链接上时它会自动显示。所以基本上,当您将鼠标悬停在 a 上时,表单会将其规则 display: none 更新为 display: block

当我开始填写字段时,浏览器会根据之前的输入显示建议。但是,当我想选择建议时,元素 再次将自身设置为 display: none

我不想设置 autocomplete="off"

建议不言而喻
enter image description here

悬停建议时整个表单消失
enter image description here

示范

* {
  box-sizing: border-box;
}

form {
  display: none;
}

div:hover form {
  background-color: #f1f1f1;
  display: block;
  padding: 15px;
}
<p>Fill in a sample username and password, after that you get an fiddle error. Just reload the fiddle and then the autocomplete shows you suggestions</p>

<div class="hover">
  <span>HOVER TO SHOW LOGIN FORM</span>
  <form method="post">
    <input type="text" id="username" name="username" placeholder="username" />
    <input type="password" id="password" name="password" placeholder="password" />
    <input type="submit" name="login" value="login" />
  </form>
</div>

如何防止这种行为?

最佳答案

尝试在输入焦点时移除悬停 a fiddle

$('input').blur(function() {
   $('.wrapper').addClass('hover');
  })
  .focus(function() {
    $('.wrapper').removeClass('hover');
  });

更新
您也可以添加 mouseleave,以便在鼠标输入结束时添加悬停类
updated fiddle

$('input').blur(function() {
   $('.wrapper').addClass('hover');
  })
  .mouseleave(function() {
    $('.wrapper').addClass('hover');
  })
  .focus(function() {
    $('.wrapper').removeClass('hover');
  });

关于html - 悬停自动完成建议使元素消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46003288/

相关文章:

javascript - 在页面向下滚动时将 pageYOffset 值添加到 div 的位置

html - 重叠 child 而不是溢出

css - 使用 :hover causes a flicker in Firefox. 增加链接的字体大小 为什么?

html - 图像在悬停时弹出容器

javascript - 如何在单击时切换按钮内的两个文本

javascript - safari 中未定义的 audio.canPlayType

CSS Div 定位内容

javascript - 将鼠标悬停在元素上时使功能停止

javascript - R 中的 MVC 模式

php - 如何在html表格中添加css类的新记录,数据存储在Mysql中?