2015-10-20 64 views
0

所有:如何綁定多個處理程序,同一事件中的jQuery

我不知道如何可以綁定一個元素上幾個處理函數相同的事件,例如:

$("#this_element").on("click.action1", function(){ 
/* first thing to run when click this element */ 
}).on("click.action2", function(){ 
/* second thing to run when click this element */ 
}).on("click.action3", function(){ 
/* last thing to run when click this element */ 
}) 

感謝

+0

您正在將處理程序綁定到不同名稱空間中的事件。你真的打算這麼做嗎? – Barmar

+1

http://stackoverflow.com/questions/1491718/jquery-more-than-one-handler-for-same-event –

+0

@Barmar這只是我的猜測,我認爲當我綁定第二個,如果我仍然使用相同的事件名稱字符串,我會覆蓋現有的。 – Kuan

回答

0

將它們全部合併爲一個函數:

$("#this_element").on("click", function() { 
    // first thing 
    // second thing 
    // third thing 
}); 
+0

大聲笑謝謝!我不能這樣做的原因是因爲處理函數有條件地綁定到元素,並不總是三個部分在一起,有時我需要綁定其中一個並刪除 – Kuan

+0

您可以更新問題以顯示代碼的真實結構,包括條件? – Barmar

+0

我沒有把真正的代碼放在那裏的原因是因爲我不知道應該在那裏放置什麼代碼,現在,這只是我的設計思想 – Kuan

相關問題