简体   繁体   English

悬停或单击一个div以影响另一个div

[英]Hover or click on one div to affect another

I'd like to have ability to hover on one div to affect another. 我希望能够将鼠标悬停在一个div上以影响另一个div。

<div id="a">1</div>
<div id="b" style="display:none;">2</div>

How to hover #a and make #b appear? 如何悬停#a并使#b出现? I'd like a css3 solution but jquery also is good. 我想要一个css3解决方案,但jquery也很好。

This works in chrome: 这适用于chrome:

http://jsfiddle.net/iambriansreed/uQfKw/ http://jsfiddle.net/iambriansreed/uQfKw/

HTML: HTML:

<div id="a">1</div>
<div id="b">2</div>

CSS: CSS:

#a + #b {
    display: none;

}
#a:hover + #b {
    display: block;

}
$("#a").on({
     mouseenter: function() {
         $("#b").show();
     },
     mouseleave: function() {
         $("#b").hide();
     },
     click: function() {
         alert('Holy crap! Someone clicked me!');
     }
});

If you place #b inside #a, you could do it with CSS, otherwise the jQuery solution above should work (with jQuery 1.7+). 如果你把#b放在#a里面,你可以用CSS来做,否则上面的jQuery解决方案应该可以工作(使用jQuery 1.7+)。

A css solution would need them to be like this: 一个css解决方案需要它们是这样的:

<div id="a">1
    <div id="b">2</div>
</div>

the css to show the block would be: 显示块的css将是:

#a:hover #b{
    display: block;
}

You can see both of the examples here: http://jsfiddle.net/skip405/L93x7/ 你可以在这里看到两个例子: http//jsfiddle.net/skip405/L93x7/

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

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