(I'm aware there's a myriad of CSS selector questions already, yet I couldn't find an answer to this one, unfortunately; so please bear with me.)

我有具有以下属性的HTML语言:

  • 有些 node 将始终可见.它们可以嵌套.
  • 这些 node 中的每个 node 总是恰好有一个"info子 node ",只有在其父 node 悬停时才会显示该 node .(可能存在其他非INFO子 node ,因为这些子 node 可以嵌套).
  • 我要only the info for the currently hovered node to be shown exclusively美元.

示例(这里是JSFiddle):

.always-visible {
  display: block;
}

.info-on-parent-hover {
  background-color: #ddd;
  font-weight: normal;
  font-style: italic;
  display: none;
}

.always-visible {
  font-weight: bold;
  background-color: #777;
}

.always-visible:hover {
  background-color: #3377cc;
}

.always-visible:hover>.info-on-parent-hover {
  display: block;
}
<div class="always-visible">
  node A [top-level node]
  <div class="info-on-parent-hover">some info about A</div>
</div>

<div class="always-visible">
  node B [top-level node]
  <div class="info-on-parent-hover">some info about B</div>

  <div class="always-visible">
    node C [child node of B]
    <div class="info-on-parent-hover">some info about C</div>
  </div>

  <div class="always-visible">
    node D [child node of B]
    <div class="info-on-parent-hover">some info about D</div>
  </div>
</div>

到目前为止,这或多或少是有效的,但我的目标是一次只显示一个"信息 node "(即属于其父 node 的 node ).所以:

I select only the 100 whose parent is currently hovered(而不是它的祖先、sibling 姐妹或后代的那些;即,当 node D悬停时, node B的信息不能也被显示)怎么可能呢?

推荐答案

你正在犯一个基本的错误你的悬停父母有一个孙子孙女,简单地设定一个sibling 姐妹悬停条件,这会奏效的 jsfiddle link

.always-visible {
    display: block;
}
.info-on-parent-hover {
  background-color: #ddd;
  font-weight: normal;
  font-style: italic;
  display: none;
}
.always-visible {
  font-weight: bold;
  background-color: #777;
}
.always-visible .text-to-hover:hover {
  background-color: #3377cc;
}
.always-visible .text-to-hover:hover + .info-on-parent-hover {
    display: block;    
}
<div class="always-visible">
  <div class="text-to-hover">
  node A [top-level node]</div>
  <div class="info-on-parent-hover">some info about A</div>
</div>

<div class="always-visible">
  <div class="text-to-hover">
  node B [top-level node]
  </div>
  <div class="info-on-parent-hover">some info about B</div>
  
  <div class="always-visible">
  <div class="text-to-hover">
    node C [child node of B]
    </div>
    <div class="info-on-parent-hover">some info about C</div>
  </div>

  <div class="always-visible">
  <div class="text-to-hover">
  node D [child node of B]</div>
    <div class="info-on-parent-hover">some info about D</div>
  </div>
</div>

Html相关问答推荐

如何增加行背景的宽度而不影响上面的内容?""' HTML CSS

我如何 Select 外部html文件元素作为选项,并显示在一个div与jQuery?

Select 包含iframe的图形<><>

容器内的SVG图像没有响应

如何生成随机字符串的字母数字字符集长度到html跨度?

从html文件中提取元素的Python BeautifulSoup

如何制作';在第';页中搜索;是否发现多个元素中的单词?

如何解决水平塌陷问题?

配置了 HTML 视口的 Google Apps 脚本移动网络应用程序无法正确显示

如何在悬停时使矩形按钮上的边框变圆

水平填充容器内的空间

如何并排放置部分?

输入框不是全宽

将 HTML 元素粘贴到容器底部,而不剪裁其顶部

如何根据行中的其中一列将行居中?

文本溢出:省略号不适用于 flexbox

如何向上移动图像并溢出图像的一部分而另一部分不溢出

文本输入在 Chrome 中保持水平滚动,带有文本溢出:省略号

CSS:如何在模糊的背景上剪切文本?

是否可以在换行时向锚标记添加填充?