注意动态添加类 [英] Watch for Dynamically Added Class

查看:94
本文介绍了注意动态添加类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果将一个类动态添加到元素中,是否有监听器可以观察并运行代码?我正在使用WordPress CMS和插件,我正在使用我们动态添加一个类,我想在发生这种情况时发现并运行一些自定义代码。

Is there a listener where I can watch and run code if a class is dynamically added to an element? I'm on a WordPress CMS and a plugin I'm using us adding a class dynamically, I want to catch on when that happens and run some custom code.

更改/ onChange似乎不起作用:

change / onChange doesn't seem to work:

$('#test').change(function(){
    alert('test');
});

我可以添加另一个能够捕获的侦听器吗?

Is there another listener I can add that will catch this?

我认为一开始并不重要,但动态类是在ajax调用后添加的。我目前正在尝试使用 ajaxComplete(),但没有运气。

I didn't think it mattered at first, but the dynamic class(es) are being added after an ajax call. I'm currently trying to work with ajaxComplete() but not having any luck.

推荐答案

我能够使用 ajaxComplete()来查找通过ajax添加的动态类。

I was able to use ajaxComplete() to find the dynamic class being added via ajax.

$( document ).ajaxComplete(function(){
    if ($('#test').hasClass('invalid')){...}
}

对任何感兴趣的人 - 我正在使用WordPress的联系表格7并使用上述代码如果我的WPCF7表单无效,请运行自定义jquery。

To anybody interested - I'm using Contact Form 7 with WordPress and using the above code to run custom jquery if my WPCF7 form is invalid.

这篇关于注意动态添加类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆