write a function onchange of some attribute
开发者_如何学Pythoni need to write a function onchange of some attribute, but when i wrote the following
$("#handle_valueAA").attr("aria-valuenow").change(function(u)
{
...
})
it doesn't work. what is the reason?
Thanks
The reason is simple. The change event is supported for form elements only - it will not work on attributes. If you want to catch attribute modifications you need to look into the DOM mutation event DOMAttrModified
and the IE proprietary event onpropertychange
.
The change
event fires when the user changes the value of a form control.
It isn't a generic event that can be associated with an attribute so it will fire when JS changes that attribute. There is nothing (at least nothing that sees wide support) that can do that.
You will either need to modify whatever functions change the attribute, or use setInterval
to poll the attribute looking for changes.
Changes (by other Javascript code, presumably) to attribute values don't trigger a "change" event.
If you have control of the code that changes that attribute, then you could instead have it trigger a custom event ("aria-changed" or whatever), and then bind a handler for that:
$('handle_valueAA').bind('aria-changed', function() { ... });
edit — or check out Andy E's answer
精彩评论