How do I find which JavaScript is changing an element's style?

大兔子大兔子 提交于 2019-11-28 06:15:00

If you're sure it's being set on the inline style and not as a consequence of a stylesheet rule, you can detect changes using the non-standard Mozilla watch() method:

document.body.style.watch('color', function(name, v0, v1) {
    alert(name+': '+v0+'->'+v1);
});
document.body.style.color= 'red';

You can put debugger; in the watcher function and look up the call stack in Firebug to see where the change was triggered.

You can also right click on the element in the HTML panel before the style is set and select break on Attribute Change. Script panel must be enabled.

Simon

On request from this question:

If you have firefox you can check the "Break on Attribute Change" option in the HTML tab. Just right click the target element and the menu will pop up. After that, resize the window and it will break in the script line where the attribute is changed.

You can open the script view and search for ".style" in the searchbox.

I think this is what is default tool for the job, although it has limited debugging capabilities: https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver

Also, make sure Ad Blocker is not responsible.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!