CSS属性更改侦听器

CSS Property Change Listener

本文关键字:侦听器 属性 CSS      更新时间:2023-09-26

有任何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)}
    }
  }
}

不赞成使用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);

我想你正在寻找这个:

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/