且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

当使用Jquery更改css属性时检测事件

更新时间:2022-10-21 22:47:46

是的,您可以。 DOM L2 Events模块定义了突变事件一>;其中之一 - DOMAttrModified 是您需要的。当然,这些并没有得到广泛的实施,但至少在Gecko和Opera浏览器中得到了支持。



尝试一下这些内容:

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

document.documentElement.style.display ='block';

您也可以尝试使用 IE的propertychange事件替换为 DOMAttrModified 。它应该允许可靠地检测 style 更改。


Is there a way to detect if the "display" css property of an element is changed (to whether none or block or inline-block...)? if not, any plugin? Thanks

Yes you can. DOM L2 Events module defines mutation events; one of them - DOMAttrModified is the one you need. Granted, these are not widely implemented, but are supported in at least Gecko and Opera browsers.

Try something along these lines:

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

document.documentElement.style.display = 'block';

You can also try utilizing IE's "propertychange" event as a replacement to DOMAttrModified. It should allow to detect style changes reliably.