Any event to detect when the “Class” attribute is

2020-05-01 07:28发布

I have a "div" control with some id="myDiv" class="myClass" var var1=10; in Javascript. Based on user action I will change the class for this control from "myClass" to "newClass" so when this has happened I want to change the value of var1 to 20.

So how would I recognize that change in Class?

2条回答
贪生不怕死
2楼-- · 2020-05-01 07:50

You may use onpropertychange(IE) and DOMAttrModified (others)

查看更多
疯言疯语
3楼-- · 2020-05-01 07:51

You'll have to use 3 things:

  1. onpropertychange event, for IE < 9
  2. DOMAttrModified, works in IE9, Opera, Firefox, and not regrettably in all others as Dr.Molle suggests
  3. Everything else - must resort to setInterval loop, that checks if value changed, this includes webkit browsers (chrome and safari), since they still don't have DOMAttrModified support, so - you'll have to resort to loop check for those browsers.

There is jQuery plugin which implements this (there is also a demo on the page):

http://darcyclarke.me/dev/watch/

查看更多
登录 后发表回答