我正在寻找一种方法来查找容器内所有通过选择器附加了事件处理程序的元素。这需要包括委托(delegate)/现场事件。
在以下示例中,选择器应找到所有 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");
});
最佳答案
循环访问与选择器关联的所有事件
$.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() );
});
});
关于javascript - 查找附加事件的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14375373/