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 form "myClass" to "newClass" so when this is happened i want to change the value of var1 to 20.
So how would i recognize that change in Class.
Thanks in advance
You may use onpropertychange(IE) and DOMAttrModified (others)
You'll have to use 3 things:
- onpropertychange event, for IE < 9
- DOMAttrModified, works in IE9, Opera, Firefox, and not regrettably in all others as Dr.Molle suggests
- 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):
来源:https://stackoverflow.com/questions/5904648/any-even-to-detect-when-the-class-attribute-is-changed-for-a-control