开发者

Hover won't work when show/hide non-parent&child element

开发者 https://www.devze.com 2023-03-04 04:50 出处:网络
I have a code like this <ul id=\"mainmenu\"> <li><a href=\"#\">mainlink1</a></li>

I have a code like this

<ul id="mainmenu">
 <li><a href="#">mainlink1</a></li>
 <li><a href="#">mainlink2</a></li>
 <li><a href="#">mainlink3</a></li>
<ul>

but i want the 'mainlink2' has a submenu, I don't want to place it like child because when i set the submenu to be 'absolute' i can't fit its width to the screen and be the center so i create a div outside the 'ul' like this ...

开发者_如何学JAVA <div>
  <ul id="submenu">
   <li>sub1</li>
   <li>sub2</li>
   <li>sub3</li>
  </ul>
 </div>

when i call javascript .hover i can't see the submenu. i try to add a class to "submenu" by remember the index of mainmenu and when it match i add class to show the submenu, when not hover i remove class. But the hover function can't do this when i leave a mouse from main menu the submenu is hide.


Try this:

HTML:

<ul id="mainmenu">
 <li><a href="#">mainlink1</a></li>
 <li class="mainlink2"><a href="#">mainlink2</a></li>
 <li><a href="#">mainlink3</a></li>
</ul>

<div class="submenu" style="position:absolute;background:#ddd; margin-left:-250px;left:50%;display:none;width:500px">
  <ul id="submenu">
   <li>sub1</li>
   <li>sub2</li>
   <li>sub3</li>
  </ul>
 </div>

JQUERY:

$(function(){

    $('.mainlink2').mouseenter(function(){
        $('.submenu').show(500);
    }).mouseleave(function(){
        $('.submenu').hide(500);
    });

});
0

精彩评论

暂无评论...
验证码 换一张
取 消