开发者

jquery event listening, non-standard events

I'm attempting to build a way for my selectors to 'listen' to 'global' events that are beyond the typical 'click' 'change' 'submit' etc. I've explored the various 'eventmanagers' that I could find, and they're all still designed for forms. Is there any way to do something like this for non-standard (i.e. custom) events? The goal is to have selectors subscribe to an event, and then be able to trigger it in one place and it will raise it for everything subscribed to it.

Inserting example for demonstration.

 return this.each(function () {
  $(this).live('ON_CONTENT_CHANGING', function (e) {
   $(this).block({
    overlayCSS: { opacity: 1, color: '#000' },
    timeout: 800
   });

   e.preventDefault();
  });

 开发者_开发问答 $(this).live('ON_CONTENT_CHANGED', function (e) {
   $(this).sliding();
   $(this).unblock();

   e.preventDefault();
  });

// rest of plugin...

$("*").trigger('ON_CONTENT_CHANGING');


This is built into jQuery, for example:

$(".button").bind("myEvent", function() {
  alert("myEvent fired on " + this.id);
});

Trigger by:

$("*").trigger("myEvent");

Or on a specific element (or any selector, like everything else jQuery):

$("#myButton").trigger("myEvent");

This uses .bind() and .trigger(). As Jacob points out in comments, you can also bind with .live() to custom events in 1.4+, like this:

$(".button").live("myEvent", function() {
  alert("myEvent fired on " + this.id);
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜