2015-02-08 150 views
0

我有一個下拉菜單,我想要的行爲,當點擊菜單外,它關閉。如何關閉菜單時點擊它

這是我的jQuery代碼:

 <script> 
    $(document).ready(function(){ 
     $(".a-for-noti-scroll").on("click",function(){ 
      $("#nano").toggleClass("x"); 
      $(".top-triangle-noti-scroll").toggleClass("y"); 
      e.stopPropagation(); 
     }); 
     $("body").on("click",function(e){ 
      $('#nano').css('visibility', 'hidden'); 
      $(".top-triangle-noti-scroll").css('display', 'none'); 
      e.stopPropagation(); 
     }); 
    }); 
    </script> 

但是,這是行不通的。

我試着.hide()body,但這樣菜單不會打開。 我該怎麼辦?

+2

嘗試使用$(document).on(「click」 – Scottie 2015-02-08 06:26:48

+0

並確保添加'e'作爲參數('...「單擊」,函數(e){...')。 – JCOC611 2015-02-08 06:27:26

+0

親愛的朋友這不是工作,我添加文件,而不是工作,而是添加文件,而不是工作 – arezoo 2015-02-08 06:56:43

回答

0

你可能有一個邏輯錯誤,嘗試這樣的例子:

$(document).ready(function(){ 

    // click inside will leave it open 
    $("#noti").on("click",function(e){ 
     $('#noti').css('display','block'); 
     e.stopPropagation(); 
    }); 

    // just for this example to open the "menu" 
    $("#top").on("click",function(e){ 
     $("#noti").css('display','block'); 
     e.stopPropagation(); 
    }); 

    // any click around closes 
    $(document).on("click",function(e){ 
     $('#noti').css('display', 'none'); 
     e.stopPropagation(); 
    }); 
}); 

這裏是一個working fiddle

這僅僅是對clickHandler堆疊的例子。

+0

你的意思是邏輯錯誤? – arezoo 2015-02-08 11:47:26