CSS:悬停在一个DIV中,如何显示另一个DIV的信息?
得到了一份包含 3 项的清单。将鼠标悬停在项目上时,将显示信息。 在同一个
中工作正常。 显示来自另一个
的信息怎么样,我想这应该是可能的,不是吗?最初仅显示三个列表。 当将鼠标悬停在“清单 2”上时,可以显示“#special”的内容,没有问题。
将鼠标悬停在“清单 3”上时,不会显示“#AlsoSpecial”的内容,为什么? 有补救办法吗?
HTML 示例:
<div id="top">
<ul>
<li class="left">listing 1</li>
<li class="center">listing 2</li>
<li class="right">listing 3</li>
<li><div id="special">
<p>bla1bla1bla1</p>
</div></li>
</ul>
</div>
<div id="content">
<div id="AlsoSpecial">
<p>bla2bla2bla2</p>
</div>
</div>
CSS 示例:
#top > li {display:inline;}
/*this works fine*/
div#special {display:none;}
.center:hover div#special {display:block;}
/*this does not work, WHY?*/
div#AlsoSpecial {display:none;}
.right:hover div#AlsoSpecial {display:block;}
Got a list with 3 items. Upon hovering over a item information shall be displayed.
Works fine within same <DIV>
.
How about displaying information from another <DIV>
, I guess this should be possible, isn't it?
Initially only the three lisitings are displayed.
When hovering over 'listing 2' the content of '#special' can be displayed, no problem.
When hovering over 'listing 3' the content of '#AlsoSpecial' is not displayed, Why?.
Is there a remedy?
Sample HTML:
<div id="top">
<ul>
<li class="left">listing 1</li>
<li class="center">listing 2</li>
<li class="right">listing 3</li>
<li><div id="special">
<p>bla1bla1bla1</p>
</div></li>
</ul>
</div>
<div id="content">
<div id="AlsoSpecial">
<p>bla2bla2bla2</p>
</div>
</div>
Sample CSS:
#top > li {display:inline;}
/*this works fine*/
div#special {display:none;}
.center:hover div#special {display:block;}
/*this does not work, WHY?*/
div#AlsoSpecial {display:none;}
.right:hover div#AlsoSpecial {display:block;}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当一个元素是另一个元素的同级元素时,您应该使用 + 选择器。
所以,这样做应该可以做到。-
但是,请注意 UL 元素只需要内部的 LI 元素,添加不同的东西是非法的,你不应该这样做。
我建议您将 DIV 元素放置在要悬停的元素内部。
因此,更好的解决方案是
...使用 CSS 选择器进行样式设置。 -
如果您希望拥有多个 DIV,也可以考虑使用 class="special" 而不是 ID对悬停事件做出反应...请记住,ID 是唯一的,不能在多个元素中使用。
如果你想让一个div与CSS“不匹配”,你就必须使用Javascript,这里有一个在Javascript中执行此操作的示例。-
我即时输入了代码,但没有尝试。我希望它能正常工作,如果它不起作用,请告诉我。
问候。
When an element is sibling of another one, you should use the + selector.
So, doing this should do it.-
However, note that UL elements expect only LI elements inside, adding something different is illegal and you should not.
I recommend you to place the DIV element inside of the element you want to hover.
So a much better solution would be
...styled with the CSS selector.-
Consider also using class="special" instead of ID, if there is any posibility that you want to have more than one DIV reacting to that hover event... remember that IDs are unique and cannot be used in more than one element.
If you want to make appear a div that is “unmatchable” with CSS, you would have to use Javascript, an example for doing this in Javascript here.-
I typed the code on the fly and did not try it. I hope it works alright, if it does not work please let me know.
Greetings.
它不起作用,因为 id 为“AlsoSpecial”的
div
不是任何类为“right”的元素的子元素 - 但您试图使用 后代选择器。我认为使用标准 CSS 选择器你将很难匹配任何不属于“兄弟”或“后代”关系的东西。
在这种情况下我会求助于 Javascript。
It doesn't work because the
div
with id "AlsoSpecial" is not a child element of any element with class "right" - but you are trying to match it using the descendant selector.I think using standard CSS selectors you will have a hard time matching anything that is not in a "sibling" or "descendant" relationship.
I would resort to Javascript in this case.
提供的两个答案都是正确的,但我决定使用尽可能少的代码(可能有点过时),如下所示:
谢谢你们。
Both answers provided are correct, but I decided to use the least code possible (may be a bit old fashioned) as follows:
Thank you both.