【问题标题】:Break when style has changed in Chrome Dev Tools在 Chrome 开发工具中更改样式时中断
【发布时间】:2012-10-29 12:17:03
【问题描述】:

我只是想知道是否有人知道是否可以在 Chrome 开发工具中特定元素的特定 css 属性上添加断点,即当#mydiv's height 属性发生更改时,中断。

【问题讨论】:

  • 样式属性更改很可能是页面布局的一部分。可能没有相关的 JavaScript 调用堆栈,所以我想知道当这样的断点命中时你会看到什么?
  • @YurySemikhatsky 中断样式属性更改时,您可以看到调用堆栈,以便确定导致更改的原因。

标签: debugging google-chrome-devtools


【解决方案1】:

您只能使用元素面板上下文菜单的Break on... | 中断所有内联样式 (<div style="height: 20px; width: 100%">) 更改。 Attributes modifications.

【讨论】:

  • 这似乎对我不起作用:(我看到style="overflow: hidden;"被添加到我的<body>标签中,它有一个属性修改断点,但我什么也没得到。我最终找到了什么不过添加它是在加载 iframe 时,所以我猜 Chrome 不能正确处理这种情况:iframe.attr("onload", "this.style.visibility = 'visible'; parent.document.body.style.overflow = 'hidden';");
  • 感谢您的关注!欢迎您在crbug.com/new 提交一个错误,并附上详细的重现步骤。附加到错误的失败案例会有所帮助。
  • 我有带有样式属性的 div,当我将此 div 更改为隐藏时,通过 display:none 属性添加。但是断点对我不起作用...
  • 要点:你只能在所有内联样式上添加break。谢谢@AlexanderPavlov
【解决方案2】:

你可以这样做:

function observe(el, property) {
    var MutationObserver = window.WebKitMutationObserver;

    var observer = new MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
            console.log('old', mutation.oldValue, 'new', mutation.target.style.cssText, 'mutation', mutation);
            if (mutation.attributeName == property) debugger;
        });
    }
    );

    var config = {
        attributes: true,
        attributeOldValue: true
    }

    observer.observe(el, config);
}

然后您可以像这样在样式更改上设置断点:observe(element, "style")

它会在更改时中断,并在控制台中打印旧值和新值。

【讨论】:

  • 在现代浏览器上,这是迄今为止最好的解决方案,因为它允许您(如果您对上面的代码进行一些编辑)仅在特定的 CSS 属性更改时中断,而忽略对其他属性的更改CSS 属性。
  • 它触发了,太棒了!但是堆栈跟踪仅显示要观察的两个引用。我如何确定它发生在代码中的哪个位置?
  • 确保在 Inspector 的设置中没有选中“禁用异步堆栈跟踪”(如果您使用 Chrome)。
  • 您的函数似乎试图通用而不是特定于"style" 属性。在这种情况下,也许它应该使用类似newValue = mutation.target[mutation.attributeName] 而不是mutation.target.style.cssText。但是为什么mutation.oldValuenewValue.cssText 格式的字符串而不是CSSStyleDeclarationnewValue 这样的对象?如何在没有“如果"style",使用.cssText,如果...,使用...”等内容的情况下获得oldValue 格式的新值?
  • 您可以使用通用的mutation.target.getAttribute(mutation.attributeName) 代替'style'-特定的mutation.target.style.cssText,参见this thread
猜你喜欢
  • 2017-12-19
  • 1970-01-01
  • 1970-01-01
  • 2011-10-14
  • 1970-01-01
  • 2020-06-25
  • 1970-01-01
  • 2014-08-04
  • 2021-01-09
相关资源
最近更新 更多