admin管理员组文章数量:1313599
I'm trying to trigger an event handler when a script modifies an input
element (text field.) On Internet Explorer, I can use the onpropertychange
event, but on Firefox and other browsers there is no such event. So, according to the W3C docs, it seems the DOMAttrModified
event does exactly what I want. But it doesn't fire in Firefox 11
.
Here's a simple code snippet which reproduces the problem. I have an input text field, and a button which adds a character to the value
attribute of the input text field. Clicking on the add char
button should cause the DOMAttrModified
event to fire:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function addChar() {
var q = document.getElementById("query");
q.value += "X";
}
function loadevents() {
var q = document.getElementById("query");
q.addEventListener("DOMAttrModified", function() {alert("DOMAttrModified event!");
}, false);
}
</script>
</head>
<body onLoad="loadevents()">
<input type="text" id="query">
<br>
<input type="submit" value="add char" onclick="addChar()">
</body>
</html>
But it doesn't. Any idea what I'm doing wrong? (I know that DOM Level 3 deprecates this event, but there doesn't seem to be a viable alternative right now. As far as I know, Firefox 11 still supports it.)
I'm trying to trigger an event handler when a script modifies an input
element (text field.) On Internet Explorer, I can use the onpropertychange
event, but on Firefox and other browsers there is no such event. So, according to the W3C docs, it seems the DOMAttrModified
event does exactly what I want. But it doesn't fire in Firefox 11
.
Here's a simple code snippet which reproduces the problem. I have an input text field, and a button which adds a character to the value
attribute of the input text field. Clicking on the add char
button should cause the DOMAttrModified
event to fire:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function addChar() {
var q = document.getElementById("query");
q.value += "X";
}
function loadevents() {
var q = document.getElementById("query");
q.addEventListener("DOMAttrModified", function() {alert("DOMAttrModified event!");
}, false);
}
</script>
</head>
<body onLoad="loadevents()">
<input type="text" id="query">
<br>
<input type="submit" value="add char" onclick="addChar()">
</body>
</html>
But it doesn't. Any idea what I'm doing wrong? (I know that DOM Level 3 deprecates this event, but there doesn't seem to be a viable alternative right now. As far as I know, Firefox 11 still supports it.)
Share Improve this question edited Mar 27, 2022 at 13:35 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked May 2, 2012 at 17:25 Channel72Channel72 24.7k35 gold badges115 silver badges187 bronze badges2 Answers
Reset to default 7Changing the value in an input doesn't fire the DOMAttrModified
event, that's all..
You need to change the attribute of the input node, not the property of the variable.
It's like the difference between the two jQuery functions: .prop
and .attr
Read:
- Which HTMLElement property change generates DOMAttrModified?
- this forum discussion
(repeating my answer from Which HTMLElement property change generates DOMAttrModified? here, because it's relevant to this question):
Please also note that NO DOMAttrModified events will be fired when the 'disabled' attribute is set. So if your event is not firing, that could be the reason. This also goes for the IE-only 'onPropertyChange' event.
本文标签: javascriptDOMAttrModified doesn39t fireStack Overflow
版权声明:本文标题:javascript - DOMAttrModified doesn't fire - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741943794a2406296.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论