Firing events on CSS class changes in jQuery

后端 未结 13 921
天涯浪人
天涯浪人 2020-11-22 04:45

How can I fire an event if a CSS class is added or changed using jQuery? Does changing of a CSS class fire the jQuery change() event?

13条回答
  •  一生所求
    2020-11-22 04:59

    I would suggest you override the addClass function. You can do it this way:

    // Create a closure
    (function(){
        // Your base, I'm in it!
        var originalAddClassMethod = jQuery.fn.addClass;
    
        jQuery.fn.addClass = function(){
            // Execute the original method.
            var result = originalAddClassMethod.apply( this, arguments );
    
            // call your function
            // this gets called everytime you use the addClass method
            myfunction();
    
            // return the original result
            return result;
        }
    })();
    
    // document ready function
    $(function(){
        // do stuff
    });
    

提交回复
热议问题