【发布时间】:2017-01-15 17:06:25
【问题描述】:
我知道我可以将MutationObserver 用于内联样式,但我想知道是否有一种方法可以检测外部类样式是否已被 chrome 的开发工具更改以及更改是什么。在此示例中,将字体颜色更改为绿色:
这是我能想到的最好的。 https://jsfiddle.net/cotssc7e/
var elem1 = document.getElementById("elemId");
var this_id = $('#elem-container').attr('id');
var style = window.getComputedStyle( document.getElementById(this_id), "")
function getTheStyle(){
var elem = document.getElementById("elem-container");
var theCSSprop = window.getComputedStyle(elem,null).getPropertyValue("color");
document.getElementById("output").innerHTML = theCSSprop;
}
setInterval(function() { getTheStyle() }, 1000)
我用过这样的 MutationObserver:
var MutationObserver = window.MutationObserver;
var target = document.querySelector('#page');
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
console.log(mutation);
});
});
var config = { attributes: true, childList: true, characterData: true, attributeOldValue: true, subtree:true, attributeOldValue:true, characterDataOldValue:true }
observer.observe(target, config);
但是如果您查看原始照片,MutationObserver 不会从开发工具中检测到类样式更改,因为我认为 DOM html 没有更改。我尝试在 css 本身上运行 MutationObserver,但 chrome 并没有真正改变 css,它们只改变了 computedStyle。以及如何以及发生了什么变化是我想要弄清楚的。
【问题讨论】:
-
那么问题到底是什么?你知道你可以使用 MutationObserver 并且你有每秒轮询的代码......你实际上想解决什么?
-
轮询是最好的选择吗?
-
我试图简单地返回更改的类以及更改的方式。
-
轮询可能是检测所有变体中 CSS 更改的唯一方法。
-
有没有办法检测选择了哪个元素,所以我至少可以检测到该元素中的 CSS 更改?
标签: javascript css google-chrome