开发者

javascript bind an event to a change of classname

开发者 https://www.devze.com 2023-04-11 15:16 出处:网络
Is it possible in javascript / jQuery to bind a开发者_StackOverflow function to a DOM element\'s classname changing or being assigned a new class?No, there\'s no broadly-supported event fired when the

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


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).

0

精彩评论

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

关注公众号