简体   繁体   中英

javascript bind an event to a change of classname

在javascript / jQuery中是否可以将函数绑定到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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM