最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to bind to all custom events in jQuery - Stack Overflow

programmeradmin2浏览0评论

I know it's not possible to bind to all DOM events and I know you can bind to multiple events by supplying a space-separated list.

But is it possible to bind to all custom events (preferably filtered by a wildcard pattern like 'abc*' or name-space)?

Edit: To clarify, I have created some custom widgets that respond to some custom events. For example, they all handle an event called stepReset and resets their internal models.

After I've written these, I realized events don't bubble down, so the call $(body).trigger('stepReset') basically does nothing. As a result, I am considering adding an umbrella event handler on all widgets' parent elements to propagate all relevant events down.

(I know this is not an elegant solution, but I forgot to tag elements with handlers with a mon class, so there's no easy way to use select them all.)

I know it's not possible to bind to all DOM events and I know you can bind to multiple events by supplying a space-separated list.

But is it possible to bind to all custom events (preferably filtered by a wildcard pattern like 'abc*' or name-space)?

Edit: To clarify, I have created some custom widgets that respond to some custom events. For example, they all handle an event called stepReset and resets their internal models.

After I've written these, I realized events don't bubble down, so the call $(body).trigger('stepReset') basically does nothing. As a result, I am considering adding an umbrella event handler on all widgets' parent elements to propagate all relevant events down.

(I know this is not an elegant solution, but I forgot to tag elements with handlers with a mon class, so there's no easy way to use select them all.)

Share Improve this question edited Nov 25, 2022 at 22:06 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Mar 16, 2012 at 10:29 billc.cnbillc.cn 7,3363 gold badges42 silver badges82 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

With regards to your uping edit, you can retrieve all bound events by accessing the object's data:

var boundEvents = $.data(document, 'events');

From here, you can iterate over the resulting object and check each property for your chosen wildcard character, or iterate over that property's array elements and check the namespace property of each.

For instance,

$.each(boundEvents, function () {
    if (this.indexOf("*"))   // Checks each event name for an asterisk *
        alert(this);

    // alerts the namespace of the first handler bound to this event name
    alert(this[0].namespace); 
});

If I understood you correctly, you can iterate over the special events object to get a list of custom events (including those specified in the jQuery source code). Here's an ES5 example, you will need to adapt it yourself for older browsers or use a polyfill for Object.keys:

var evts = Object.keys(jQuery.event.special).join(" ");
$("#myDiv").on(evts, function (e) {
    // your code here
});
发布评论

评论列表(0)

  1. 暂无评论