javascript将事件绑定到类名的更改 [英] javascript bind an event to a change of classname

查看:90
本文介绍了javascript将事件绑定到类名的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在javascript/jQuery中是否可以将函数绑定到DOM元素的类名更改或分配给新的类上?

Is it possible in javascript / jQuery to bind a function to a DOM element's classname changing or being assigned a new class?

推荐答案

否,更新该类时不会触发广泛支持的事件.您必须进行投票.某些浏览器支持 DOM突变事件,但从来没有已完全支持跨浏览器,请参阅链接页面上的警告.

No, there's no broadly-supported event fired when the class is updated. You'll have to poll. Some browsers support DOM mutation events, but it's never been completely supported cross-browser, see the warning on the linked page.

很显然,如果是您的代码更改了该类,则可以创建一个用于执行该操作的函数,并始终使用该函数而不是直接更改它,但是,如果您要在代码之外寻找对它的更改,除了轮询以外,我看不到其他任何方法.

Obviously, if it's your code that changes the class, you could create a function you use to do that and always use that function rather than changing it directly, but if you're looking for changes to it outside of your code, I don't see an alternative to polling.

如果进行轮询,请确保在所有目标浏览器上进行测试,并在轮询代码中实际完成尽可能少的工作.过度操作很容易(降低页面速度).

If you poll, be sure to test on all of your target browsers and do as little work actually in the polling code as you can. It's easy to over-do it (slowing down the page).

这篇关于javascript将事件绑定到类名的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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