CSS属性更改侦听器

Dre*_*rew 6 javascript css

任何建议的CSS属性更改侦听器的实现?也许:

thread =

function getValues(){
  while(true){
    for each CSS property{
      if(properties[property] != nil && getValue(property) != properties[property]){alert('change')}
      else{properties[property] = getValue(property)}
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

Kay*_*ues 5

类似的突变事件DOMAttrModified已被弃用。考虑使用 MutationObserver 来代替。

例子:

<div>use devtools to change the <code>background-color</code> property of this node to <code>red</code></div>
<p>status...</p>
Run Code Online (Sandbox Code Playgroud)

JS:

var observer = new MutationObserver((mutations) => {
  mutations.forEach((mutation) => {
    if (mutation.target.style.color === 'red') {
      document.querySelector('p').textContent = 'success';
    }
  });
});

var observerConfig = {
  attributes: true,
  childList: false,
  characterData: false,
  attributeOldValue: true
};

var targetNode = document.querySelector('div');
observer.observe(targetNode, observerConfig);
Run Code Online (Sandbox Code Playgroud)


Swi*_*ift 3

我想你正在寻找这个:

document.documentElement.addEventListener('DOMAttrModified', function(e){
  if (e.attrName === 'style') {
    console.log('prevValue: ' + e.prevValue, 'newValue: ' + e.newValue);
  }
}, false);
Run Code Online (Sandbox Code Playgroud)

如果你用谷歌搜索它,就会出现一堆东西。但这看起来很有希望:

http://darcyclarke.me/development/detect-attribute-changes-with-jquery/