簡體   English   中英

首次單擊后更改元素的onclick處理程序

[英]Changing onclick handler for an element after first click

   div.onclick = function(data, dom) {
        return function() {
           if (data.seenAlready == true) { // HACK
              $(this).children().toggle();
              return;
           }
           recursiveSearch(data, dom);

          // after this onclick, I want to assign it to a toggle like function. no clue how to do it.
       }
  }(child, mycontainer.appendChild(div));

我試圖在第一次對dom元素進行onclick之后交換onclick方法。 我只是沒有取得任何成功,似乎某種程度的關閉損失或其他原因。 我很好用jQuery。

您可以通過兩種方法來執行此操作,並且兩種方法都可以使用jQuery函數:

1)使用one API方法-這將只工作一次。 您將單擊一次,然后選擇自己的第二個處理程序,第一個處理程序將不會再次觸發,例如

$(myselector).one(function(){
    $(this).click(myotherhandler);
});

這是此API的鏈接http://api.jquery.com/one/

2)您可以選擇以下方式替換事件處理程序。

$(myselector).click(function(){
    $(this).off();
    $(this).click("secondhandler");
});

這將關閉第一個處理程序,而只會觸發第二個處理程序

檢查此jsbin:

http://jsbin.com/fekuq/1/edit?html,js,輸出

暫無
暫無

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

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