开发者

is it possible to trigger events on class change events?

开发者 https://www.devze.com 2022-12-12 18:05 出处:网络
I want to do something to an HTML element after it has been processed by some other js process over which I have no control.

I want to do something to an HTML element after it has been processed by some other js process over which I have no control.

for example, given ... ...

and some 3rd party async app does $('div.x').each(function(){ /* do stuff */; this.addClass('processed');});

Is there some strategy for registering an interest in开发者_高级运维 the div when its class changes to 'processed'?


This is probably a bad idea, but i think its better than polling...

Take a look at the jQ lib uncompressed, copy the code for addClass, removeClass, and attr.

then use $.fn.extend passing in new definitions for those methods. You really wont be changing them all that much just telling them to call this.trigger('classChange') after they have done their thing. Although in the case of attr youll need an if statment telling it to fire only if the attribute class was the target or if the key 'class' was in the hash passed in.

you can then use $(selector).bind('classChange', lambda); to provide the operation for what happens on a class change.

Ofcourse ive never tried to overload a core method before, but i dont see why you couldnt.

Might look something like this. Ive never looked at the interals so you might not be able to just include it in blocks like ove doen - you might have to modify individual switch/if cases or something...

$.fn.extend({
  addClass: function(class){
    // standard jq logic
    this.trigger('changeClass');
    // standard return
  },
  removeClass: function(class){
    // standard jq logic
    this.trigger('changeClass');
    return this;
  },
  attr: function(prop, val){
    // standard jq logic

    if(typeof prop == 'object' && typeof prop.class != 'undefined'){
      this.trigger('changeClass');
    } else if(typeof prop == 'string' && typeof val !== 'undefined' && prop == 'class'){
      this.trigger('changeClass');
    }
    return this;
  }
}); 


Not that I know of, from the series of JQuery events.

You might want to implement a interval polling check though it sounds kinda messy :P


No simple solution. Similar question here.

Check out PPK's list of browser events to see what's available.


Try this ,

(function($){

$.each(["addClass","removeClass"],function(i,methodname){
      var oldmethod = $.fn[methodname];
      $.fn[methodname] = function(){
            oldmethod.apply( this, arguments );
            this.trigger(methodname+"change");
            return this;
      }
});

})(jQuery);

To Initiate Use:

$('#demo').bind('addClass removeClass',function(event){
      alert(event.type + " event triggered!");
});

Got the solution from

http://forum.jquery.com/topic/firing-an-event-on-addclass

0

精彩评论

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