简体   繁体   English

仅在将鼠标悬停在复选框链接上时显示文本描述

[英]Only display text description when hovering over checkbox link

I'm trying to only show the "This is an item" text when hovering each checkbox link.我试图在悬停每个复选框链接时只显示“这是一个项目”文本。 For some reason, the "This is an item" text still seems to show when when checkbox link is not hovered over.出于某种原因,当复选框链接未悬停时,“这是一个项目”文本似乎仍然显示。 Is there any way to only show text when the checkbox item is hovered over?有没有办法只在复选框项目悬停时显示文本?

 var accordions = document.getElementsByClassName("accordion"); for (var i = 0; i < accordions.length; i++) { accordions[i].onclick = function() { this.classList.toggle('is-open'); var content = this.nextElementSibling; if (content.style.maxHeight) { // accordion is currently open, so close it content.style.maxHeight = null; } else { // accordion is currently closed, so open it content.style.maxHeight = content.scrollHeight + "px"; } } }
 /* Filter Display */.filter-box { background-color: #f5f5f5; display: flex; flex-direction: column; gap: 6px; margin-top: 10px; }.filter-box button { background-color: rgb(49, 49, 49); color: white; } /* Section Buttons */ button.accordion { width: 30%; background-color: black; border: none; outline: none; text-align: left; padding: 15px 20px; font-size: 14px; color: white; cursor: pointer; transition: background-color 0.2s linear; color: white; } button.accordion:after { font-family: "fontawesome"; font-size: 14px; float: right; } button.accordion.is-open:after { content: '\f056'; } button.accordion:hover, button.accordion.is-open { background-color: grey; }.accordion-content { width: 30%; background-color: rgb(255, 255, 255); border-left: 1px solid black; border-right: 1px solid whitesmoke; padding: 0 20px; max-height: 0; overflow: hidden; transition: max-height 0.2s ease-in-out; }.accordion-content li { list-style-type: none; }.accordion-content li:hover { font-weight: bold; color: red; }.item-description { display: none; }.accordion-content li:hover, .item-description { display: contents; }
 <button class="accordion">Catagories</button> <div class="accordion-content"> <div class=list-item> <li> <label> <input type="checkbox" name="checkbox" />Item1 </label> </li> <p class="item-description">This is an item</p> </div> <div class=list-item> <li> <label> <input type="checkbox" name="checkbox" />Item2 </label> </li> <p class="item-description">This is an item</p> </div> <div class=list-item> <li> <label> <input type="checkbox" name="checkbox" />Item3 </label> </li> <p class="item-description">This is an item</p> </div> </div>

text in the bottom and only initiate text when hovering beside each checkbox.底部的文本,只有在悬停在每个复选框旁边时才会启动文本。

Your HTML contains invalid markup and you have conflicting styles for your list items hover state.您的 HTML 包含无效标记,并且您的列表项 hover Z9ED39E2EA9314586B6A985A973 的 styles 冲突First, lets address the HTML issues:首先,让我们解决 HTML 问题:

  1. An <li> element may only be a child of <ul> , <ol> , or <menu> so using a <div> as the parent of list items in your current structure is invalid.<li>元素只能是<ul><ol><menu>的子元素,因此在当前结构中使用<div>作为列表项的父元素是无效的。
  2. The permitted content for list elements is Flow Content so you have to make sure only elements of that group are children of <li> tags.列表元素的允许内容是流内容,因此您必须确保只有该组的元素是<li>标记的子元素。

After reformatting your HTML so the markup was valid, (which you can always verify using W3C Validator ) and fixing some of your list item hover state styling.在重新格式化您的 HTML 以使标记有效(您始终可以使用W3C Validator 验证)并修复您的一些列表项 hover state 样式后。 The text "This is an item" displays inline with the checkboxes when hovered as expected.当按预期悬停时,文本“这是一个项目”与复选框内联显示。

This is why the text was visible before and after hover:这就是文本在 hover 之前和之后可见的原因:

.item-description {
  display: none;
}

.accordion-content li:hover,
.item-description {
  display: contents;
}

Your making the .item-description text disappear with display: none then on the next line make it visible with display: contents .您使.item-description文本随着display: none而消失,然后在下一行使用display: contents使其可见。 I assumed you meant to style the .item-description when list items were hovered so I changed this selector to be .accordion-content li:hover.item-description and that ensures text is only displayed when the list items are hovered.我假设您打算在悬停列表项时设置.item-description的样式,因此我将此选择器更改为.accordion-content li:hover.item-description并确保仅在悬停列表项时显示文本。

 var accordions = document.getElementsByClassName("accordion"); for (var i = 0; i < accordions.length; i++) { accordions[i].onclick = function() { this.classList.toggle('is-open'); var content = this.nextElementSibling; if (content.style.maxHeight) { // accordion is currently open, so close it content.style.maxHeight = null; } else { // accordion is currently closed, so open it content.style.maxHeight = content.scrollHeight + "px"; } } }
 /* Filter Display */.filter-box { background-color: #f5f5f5; display: flex; flex-direction: column; gap: 6px; margin-top: 10px; }.filter-box button { background-color: rgb(49, 49, 49); color: white; } /* Section Buttons */ button.accordion { width: 30%; background-color: black; border: none; outline: none; text-align: left; padding: 15px 20px; font-size: 14px; color: white; cursor: pointer; transition: background-color 0.2s linear; color: white; } button.accordion:after { font-family: "fontawesome"; font-size: 14px; float: right; } button.accordion.is-open:after { content: '\f056'; } button.accordion:hover, button.accordion.is-open { background-color: grey; }.accordion-content { width: 30%; background-color: rgb(255, 255, 255); border-left: 1px solid black; border-right: 1px solid whitesmoke; padding: 0 20px; max-height: 0; overflow: hidden; transition: max-height 0.2s ease-in-out; }.accordion-content li { list-style-type: none; }.accordion-content li label { margin-right: .5rem; }.accordion-content li:hover { font-weight: bold; color: red; }.item-description { display: none; }.accordion-content li:hover.item-description { display: contents; }
 <button class="accordion">Categories</button> <ul class="accordion-content"> <li class=list-item> <label> <input type="checkbox" name="checkbox" />Item1 </label> <p class="item-description">This is an item</p> </li> <li class=list-item> <label> <input type="checkbox" name="checkbox" />Item2 </label> <p class="item-description">This is an item</p> </li> <li class=list-item> <label> <input type="checkbox" name="checkbox" />Item3 </label> <p class="item-description">This is an item</p> </li> </ul>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM