开发者

How can I detect when an HTML element’s class changes? [duplicate]

This question already has answers here: 开发者_如何学运维 Firing event on DOM attribute change (6 answers) Closed 5 years ago.
<div class="current"></div>
<a href="#">text</a>

How do I detect when e.g. a .live class is added to this <div> and do something when it happened?

For example, if class is changed, then hide the link.

I have a minified script that makes changes on the <div>'s class. It’s too big for me to find the place where it happens, so I'm searching for some way to catch the class change.


There are DOM events, but they are far from perfect and not available in most browsers. In other words: Not possible (reliably). There are hacks like intervals and checking it in each iteration, but if you need to do something like this, your design is probably screwed. Keep in mind that such things will be slow and that there will always be a delay. The smaller the delay, the slower the application. Do not do that.


You may checkout the following article for some ideas.


I don't know if the class you add to the link is always the same but if so, why don't you use CSS for this.

<style type='text/css>
  .myaddedclass{display:none}
</style>


If you just need to figure this out once (i.e. not in code), Google Chrome’s web inspector shows DOM changes live. Not sure if that’d help your situation out though.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜