6

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)}
    }
  }
}
4

2 回答 2

3

我想你正在寻找这个:

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

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

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

于 2012-08-30T16:28:07.120 回答
3

类似的突变事件DOMAttrModified已被弃用。考虑改用 MutationObserver。

例子:

<div>use devtools to change the <code>background-color</code> property of this node to <code>red</code></div>
<p>status...</p>

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);
于 2017-06-01T20:30:31.117 回答