使用 Jquery 更改 css 属性时的事件检测 [英] Event detect when css property changed using Jquery

查看:29
本文介绍了使用 Jquery 更改 css 属性时的事件检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法检测元素的显示"css 属性是否已更改(更改为 none、block 或 inline-block...)?如果没有,任何插件?谢谢

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

推荐答案

注意

突变事件自这篇文章以来已被弃用已编写,可能并非所有浏览器都支持.相反,使用突变观察者.

Note

Mutation events have been deprecated since this post was written, and may not be supported by all browsers. Instead, use a mutation observer.

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

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.

尝试以下方法:

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 更改.

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

这篇关于使用 Jquery 更改 css 属性时的事件检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆