简体   繁体   English

在调试 HTML 时,有没有办法使用 JavaScript 或 jQuery 来监听 div 宽度变化?

[英]When debugging HTML is there a way to listen for div width change using JavaScript or jQuery?

When debugging HTML is there a way to listen for div width change using JavaScript or jQuery?在调试 HTML 时,有没有办法使用 JavaScript 或 jQuery 来监听 div 宽度变化? I have a div changing size due to some CSS and can't figure out where, even stepping through.由于一些 CSS 我有一个 div 改变大小,并且无法弄清楚在哪里,甚至步入。 I was wondering if there was a way to hook an event up to a div to see what is causing it to change?我想知道是否有办法将事件连接到 div 以查看导致它发生变化的原因? Even if possible, I'm not sure how how would pass stack information for what caused the size change in the first place, so this may not even be possible.即使可能,我也不确定如何首先传递堆栈信息以了解导致大小变化的原因,所以这甚至可能是不可能的。

I use Chrome, it seems to give the most details.我使用 Chrome,它似乎提供了最多的细节。

  • Download Chrome下载铬
  • Press f12按 f12
  • goto first tab [Elements]转到第一个选项卡 [元素]
  • click on the magnifying glass and go to your div单击放大镜和 go 到您的 div
  • right click on the highlighted html and select "Break on Subtree modifications"右键单击突出显示的 html 和 select “中断子树修改”

This is bad way, but I'm not sure such event exists.这是不好的方式,但我不确定这样的事件是否存在。

$(function(){
    var last = $("#your_div").width();
    setInterval(function(){
        if (last != $("#your_div").width()){
            alert('div width changed');
        }
    }, 100);
});

This guy explains how attach to the DOMAttrModified event to detect CSS changes.这个人解释了如何附加到 DOMAttrModified 事件以检测 CSS 更改。

Event detect when css property changed using Jquery 使用 Jquery 更改 css 属性时的事件检测

It's only supported in FF and Opera, but if its just for debugging it will do the trick.它仅在 FF 和 Opera 中受支持,但如果它只是用于调试,它就可以解决问题。

http://www.quirksmode.org/dom/events/ http://www.quirksmode.org/dom/events/

Firebug for FireFox allows you to attach and break on CSS and attribute changes is this what you're looking for? FireFox 的 Firebug 允许您在 CSS 上附加和断开,并且属性更改是您正在寻找的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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