2011-12-19 147 views
0
以上

请原谅标题的措辞,但总结起来很难。将鼠标悬停在元素更改元素上,然后将鼠标悬停在

这就是我想要做的

<div id="test">Test</div> 
<div id="example1" style="display:none;">Test</div> 
<div id="example2" style="display:none;">Test</div> 

#test悬停,它会导致#example1#exmaple2徘徊中止,#example1#exmaple2再次返回到无形之中时变得可见,然后。

明显的简单代码。

$('#test').hover(
    function() { 
    $('#example1,#example2').show(); 
    }, 
    function() { 
    $('#example1,#example2').hide(); 
    } 
); 

现在,这里的困境。我需要#example1#exmaple2在鼠标悬停时保持可见状态,这是不可能的,因为当鼠标离开#test时,鼠标悬停在#example1#example2上,它们会回到隐形状态,正如上面的代码所指示的那样。我怎样才能使这成为可能?

我明白包装的元素这样在一起

<div id="wrapper"> 
<div id="test">Test</div> 
<div id="example1" style="display:none;">Test</div> 
<div id="example2" style="display:none;">Test</div> 
</div> 

和更改代码

$('#wrapper').hover(
    function() { 
    $('#example1,#example2').show(); 
    }, 
    function() { 
    $('#example1,#example2').hide(); 
    } 
); 

可以解决这个问题,但我不能这样做,因为在我的实际代码, #test已经在包装下了,这是一个包装,它具有与之相关的非常具体的设计,如果我在其中包含#example1#example2,它将被销毁。

综上所述上面,我的问题是:当#test悬停和#example1#example2变得可见,我怎能#example1#example2可见离开#test即使悬停在#example1#example2

回答

0

这不行吗?

$('#test').hover(
    function() { 
    $('#example1,#example2').show(); 
    } 
    , function() { 
    $('#example1,#example2').hide(); 
    } 
); 

然后改变你的html到这样的东西来解决高度问题。

<div id="test"> 
Test 
<div id="example1" style="display:none;z-index:256;">Test</div> 
<div id="example2" style="display:none;z-index:256;">Test</div> 
</div> 
+0

正如我上文所述,我已经有'在我的代码不同的包装#test',它是高度专用的,所以'#example1'和'#example2'会毁了它。我想知道在重做我的整个代码之前是否有办法在jQuery或JavaScript中做到这一点。 – Odyssey 2011-12-19 02:19:25

+0

@Odyssey我已经添加了解决方案的另一半。 – shinkou 2011-12-19 02:31:35

+0

这给了我'位置:绝对'和'边缘顶部'的想法,它的工作方式就像我需要的一样! – Odyssey 2011-12-22 01:17:05

0

如何使用mousenter和mouseleave?

编辑... 可以包含的元素

$('#test,#example1,#example2').hover(

function() { 
    $('#example1,#example2').show(); 
}, function() { 
    $('#example1,#example2').hide(); 
}); 
+0

'mouseenter'和'mouseleave'本质上是'.hover'在jQuery中的,它仍然使我回到了原来的困境,它不能将'#test','#example1'和'#example2'包装到一起到预先存在的代码。 – Odyssey 2011-12-19 02:21:49

+0

这个怎么样? http://jsfiddle.net/pixelass/4kQjY/ – pixelass 2011-12-19 02:26:15

相关问题