簡體   English   中英

我不能在jquery上使用off函數進行click事件

[英]I can't use off function for click event on jquery

我想在jquery上做可打開的菜單。 我使用點擊方法,它的工作,但我無法關閉它。 我該怎么辦? 謝謝 :)

$("div#kkayit").on("click", function(){
    $(this).css("background-color", "pink");
});

$("div#kkayit").on("click", function(){
    $("div#kkayit").off("click");
});

您已為同一選擇器注冊了相同的事件兩次。 因此,我建議僅在下面的代碼中使用所提到的選擇器的click事件。

$("div#kkayit").one("click", function(){
    $(this).css("background-color", "pink");
});

http://api.jquery.com/one/

您可以在jquery中使用該功能toggle

http://api.jquery.com/toggle/

 $(document).ready(function(){ $("button").click(function(){ $("p").toggle(); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p> HERE Will toggle</p> <button>Toggle between hide() and show()</button> 

請嘗試以下:

$("div#kkayit").on("click", function(event){
    $(this).css("background-color", "pink");
    event.preventdefault();
});

如果我回答你的問題,請告訴我。

謝謝。 維奈

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM