Firing events on CSS class changes in jQuery

后端 未结 13 1105
天涯浪人
天涯浪人 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 05:02

    using latest jquery mutation

    var $target = jQuery(".required-entry");
                var observer = new MutationObserver(function(mutations) {
                    mutations.forEach(function(mutation) {
                        if (mutation.attributeName === "class") {
                            var attributeValue = jQuery(mutation.target).prop(mutation.attributeName);
                            if (attributeValue.indexOf("search-class") >= 0){
                                // do what you want
                            }
                        }
                    });
                });
                observer.observe($target[0],  {
                    attributes: true
                });
    

    // any code which update div having class required-entry which is in $target like $target.addClass('search-class');

提交回复
热议问题