2012-12-04 56 views
-6

这是我的菜单JavaScript功能。现在我的菜单打开,点击关闭点击&。 我想打开鼠标离开按钮时点击&关闭。如何使用鼠标点击并关闭来打开javascript菜单

$("#theme_select").click(function() { 
    if (theme_list_open == true) { 
     $(".center ul li ul").hide(); 
     theme_list_open = false; 
    } else { 
     $(".center ul li ul").show(); 
     theme_list_open = true; 
    } 
    return false; 
});​ 

即时编辑一人&顶部问题修复。但是当我想将鼠标移动到打开的菜单项目菜单被关闭。看到我完整的脚本(变更前):

<script type="text/javascript"> 

    var theme_list_open = false; 

    $(document).ready(function() { 

     function fixHeight() { 

     var headerHeight = $("#switcher").height(); 

     $("#iframe").attr("height", (($(window).height() - 1) - headerHeight) + 'px'); 

     } 

     $(window).resize(function() { 

      fixHeight(); 

     }).resize(); 

     $("#theme_select").click(function() { 

      if (theme_list_open == true) { 

      $(".center ul li ul").hide(); 

      theme_list_open = false; 

      } else { 

      $(".center ul li ul").show();    

      theme_list_open = true; 

      } 

      return false; 

     }); 

     $("#theme_list ul li a").click(function() { 

     var theme_data = $(this).attr("rel").split(","); 

     $("li.purchase a").attr("href", theme_data[1]); 
     $("li.remove_frame a").attr("href", theme_data[0]); 
     $("#iframe").attr("src", theme_data[0]); 

     $("#theme_list a#theme_select").text($(this).text()); 

     $(".center ul li ul").hide(); 

     theme_list_open = false; 

     return false; 

     }); 

    }); 

    </script> 
+0

['.hover()'](http://api.jquery.com/hover /) – PeeHaa

+0

Peehaa是你的答案吗? – VoronoiPotato

+0

@VoronoiPotato不,这是一个问题... – 2012-12-04 21:23:15

回答

2

是否这样?

例。(编辑你的元素选择,如果你知道的jQuery不够)

HTML:

<ul> 
    <li>Menu#1</li> 
    <span>Sub</span> 
    <li>Menu#2</li> 
    <span>Sub</span> 
</ul> 

的jQuery:

$("ul li").click(function() { 
    $(this).addClass("showing").next("span").show(); 
}); 

$('ul').mouseout(function() { 
    $("ul li.showing").removeClass().next("span").hide(); 
}); 

演示:http://jsfiddle.net/JcKxV/

在你的情况下编辑...要去的样子..

$("#theme_select").click(function() { 
    if (theme_list_open == false) { 
     $(".center ul li ul").addClass("showing").show(); 
     theme_list_open = true; 
    } 
    return false; 
}); 

$("#theme_select").mouseout(function() { 
    $(".center ul li ul.showing").removeClass().hide(); 
    theme_list_open = false; 
}); 

$("#theme_select").click(function() { 
    if (theme_list_open == false) { 
     $(".center ul li ul").show(); 
     theme_list_open = true; 
    } 
    return false; 
}); 

$("#theme_select").mouseout(function() { 
    if (theme_list_open == true) { 
     $(".center ul li ul").hide(); 
     theme_list_open = false; 
    } 
}); 
+0

thanck you。 im not profesional ples帮助我如何将此代码添加到我的代码 – user1460201

+0

您需要在我的代码中显示您的HTML – l2aelba

+0

evry事情是好的,但不要用鼠标关闭 – user1460201

1

[什么@PeeHaa想说的是]使用jQuery.hover()功能。

$("#theme_select").click(function() { 
    if ($("#theme_select").is(":visible")) { 
     $(".center ul li ul").hide(); 
    } else { 
     $(".center ul li ul").show(); 
    } 
    return false; 
});​ 


$("#theme_select").hover(function() { 
    //Do Nothing 
    },function(){ 
     $(".center ul li ul").hide(); //Mouse leave 
});​ 

第一个函数表示当鼠标悬停在theme_select上时执行的代码。第二个函数表示当鼠标离开theme_select时执行的代码。

+0

1-我需要打开点击,没有鼠标。 – user1460201

+0

2-如何将此代码添加到我的代码? – user1460201

+0

查看更新 - 用此替换您的代码。 –

相关问题