在jQuery中监听已选中/已禁用的更改 [英] Listen for changes of checked/disabled in jQuery

查看:78
本文介绍了在jQuery中监听已选中/已禁用的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在HTML页面中有一些对象(div,input / checkbox)。它们的checked和disabled属性的值是通过一些JS函数设置的。我想听这些属性的更改,但找不到任何函数或监听器来执行它。

I have some object (div, input/checkbox) in an HTML page. The values for their "checked" and "disabled" attributes are set via some JS functions. I want to listen to changes of these attributes, but couldn't find any function or listener to perform that.

如何添加一个侦听变化的事件监听器检查或禁用并执行一些代码(例如更改checkbox / div的样式),具体取决于属性的状态?

How can I add an event listener that listen on changes of "checked" or "disabled" and execute some code (e.g. changing style of checkbox / div) depending on the attributes' status?

谢谢

推荐答案

关于禁用更改,您可以收听DOMAttrModified事件。有关详细信息,请参阅此测试用例:
http://jsfiddle.net/4kWbp/1/

Regarding the "disabled" change, you may be able to listen to DOMAttrModified events. See this test case for details: http://jsfiddle.net/4kWbp/1/

请注意,并非所有UA都支持DOMAttrModified等DOM突变事件,并且在支持它们的那些事件中,听取它们可能会导致性能下降。

Note that not all UAs support DOM mutation events like DOMAttrModified, and in those that do support them, listening to them may cause performance to detoriate.

直接设置.checked不会触发更改事件,并且似乎也没有触发DOMAttrModified(仅在Opera中测试过,而这是在-spec-gaps在浏览器中可能非常不一致的东西。也许这是一个Opera bug。)

Setting .checked directly does not trigger "change" events, and doesn't seem to trigger DOMAttrModified either (only tested in Opera though, and this is the sort of under-specified between-the-spec-gaps stuff that might well be very inconsistent across browsers. Perhaps it's an Opera bug.)

最后的手段可能是为这些属性定义getter / setter。那会是一个相当丑陋的黑客..

The last resort would perhaps be defining getters/setters for those properties. That would be a rather ugly hack though..

这篇关于在jQuery中监听已选中/已禁用的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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