2012-12-04 54 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

查看更新 - 用此替換您的代碼。 –

相關問題