html - 悬停时隐藏外部节点元素

标签 html css

<分区>

我想在将中心元素悬停在 this sample 上时隐藏底部和顶部元素:

<div id="tc">
  <span id="ct">top</span>
  <div id="pc">
    <span id="st"></span>
    <span id="tp">center</span>
  </div>
  <span id="tt">bottom</span>
</div>

我可以用 :

隐藏底部元素
#pc:hover ~ #tt { display: none!important; }

我希望通过

对顶部元素完成相同的操作:
#ct + #pc:hover { display: none!important; }

但它不起作用,甚至破坏了我的第一条指令。

我做错了什么?

最佳答案

实际上您可以使用 jQuery 来完成,因为选择上面的 DOM 非常困难。 所以这是替代解决方案

第 1 步:在 head 标记中添加 jQuery 脚本

   <head>
    <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
   </head>

第 2 步:将此代码放在元素下方

<script>
 $( "#pc" ).hover(
   function() {
   $( "#ct" ).hide();

   }, function() {
   $( "#ct" ).show();
   }
    );
  </script>

这将完全符合您的要求,显示:“无”ID 为“ct”的 DOM 元素。

关于html - 悬停时隐藏外部节点元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55319035/

相关文章:

javascript - 允许用户在文本框中仅添加空格且电话号码不超过 11 个

javascript - 如何使用函数返回的对象?

Safari 中的 CSS 定位中断

css - 不同浏览器的字体大小差异很大

jquery - 使用漂亮的过渡效果更改背景图像 - Jquery

javascript - 如何将我的进度条百分比限制为仅 100%

html - jQuery Mobile 导航错误

javascript - 我怎样才能阻止这个 jQuery 出问题?

javascript - 根据DIV样式隐藏没有ID的DIV

html - 防止文本换行