2013-01-17 26 views
3

我正在尋找一種方法來查找通過選擇器附加到它們的事件處理程序的容器內的所有元素。這需要包括委託/現場活動。查找具有附加事件的元素

在以下示例中,選擇器應該查找所有3個按鈕,包括具有指定事件的「操作」按鈕。

<div id='container'> 
    <button id='test_btn'>Test</button> 
    <button id='live_btn_id1'>Action</button> 
    <button id='live_btn_id2'>Action</button> 
</div> 
$("#test_btn").on("click", function() { 
    $("#container").find("*").off(); 
}); 

$("#container").on('click', 'button[id^=live_btn]', function(event) { 
    alert("hello"); 
}); 
+0

聯能否請您解釋一下你爲什麼選擇這些元素?你打算怎麼處理他們?也可以有一些其他的替代品.. – Wolf

+0

可能的重複:http://stackoverflow.com/questions/661564/jquery-selector-that-search-for-events –

+1

你可以隨時添加一個類.. http:// jsfiddle .net/lollero/XNHNU/ – Joonas

回答

0

遍歷所有活動與您選擇

$.each($('#container button').data('events'), function(i, event){ 
    $.each(event, function(i, handler){ 
     //Do what you want : condition on 'click' event for example 
     console.log(handler.toString()); 
    }); 
}); 
+0

對我不起作用我得到以下錯誤:「TypeError:a未定義」 – Nuvolari

+0

也可能是錯誤的,但我相當肯定他們將事件從最近的JQuery更新之一中的「data」對象移動到某個內部位置。 – Nuvolari

相關問題