JS中onpropertychange事件和onchange事件区别

当一个HTML元素的属性用js改变的时候,都能经过 onpropertychange来捕获。例如一个 <input name="text1" id="text1" />对象的value属性被页面的脚本修改的时候,onchange没法捕获到,而onpropertychange却可以捕获。   也就是说:onpropertychange事件在用键盘每改变一下文本框的值或用js改变其值便会触发一下,而onchange只有在用键盘改变其值,而后在失去焦点(onblur)后才触发,用js改变其值不能触发!onpropertychange和onchange都无论文本框中的实际值有没有变,只要有改的相应操做就可能触发。有时当上面两时间都不能知足需求时,能够考虑只用onblur。   还有一点要注意到,当onblur和onchange事件一块儿用时,onblur会出问题。。。。详见以下   测试页面:   <html>   <head> <title>经过js改变文本框中的值后触发的事件:onpropertychange事件 </title> </head>   <body>   <div id="test1">   <b>测试onpropertychange事件和onchange事件一块儿用时: </b> <br>   <font color="red">测试结果:onpropertychange事件在用键盘每改变一下文本框的值或用js改变其值便会触发一下,而onchange只有在用键盘改变其值,而后在失去焦点后才触   发,用js改变其值不触发 </font>   <br>   <input name="haha1" type="text" onpropertychange="alert('触发了onpropertychange事件!')" onchange="alert('触发了onchange事件!')" size="30" >   <input name="testbutton1" value="经过js改变文本框中的值" type="button" onclick="document.getElementById('haha1').value='js改变文本框后的值'">   <br>   <br>   <br>   <div id="test2">   <b>测试只有onblur和onchange事件时: </b> <br>   <font color="red">测试结果:onchange先触发,onblur后触发 </font>   <br>   <input name="haha2" type="text" onblur="alert('触发了onblur事件!')" onchange="alert('触发了onchange事件!')" size="30" >   <input name="testbutton2" value="经过js改变文本框中的值" type="button" onclick="document.getElementById('haha2').value='js改变文本框后的值'">   <br>   <div>   <br>   <br>   <br>   <div id="test3">   <b>测试当onblur和onpropertychange事件一块儿用时: </b> <br>   <font color="red">测试结果:onblur好象出了问题,只要用键盘在文本框中随便输入一个值,便会触发它。多是onpropertychange把它惹毛了。。。^-^ </font>   <br>   <input name="haha3" type="text" onblur="alert('触发了onblur事件!')" onpropertychange="alert('触发了onpropertychange事件!')" size="30" >   <input name="testbutton3" value="经过js改变文本框中的值" type="button" onclick="document.getElementById('haha3').value='js改变文本框后的值'">   <br>   <div>   <br>   <br>   <br>   <div id="test4">   <b>测试有onblur、onpropertychange事件和onchange事件一块儿用时: </b> <br>   <font color="red">测试结果:onblur在和onpropertychange一块儿用时的问题仍然存在 </font>   <br>   <input name="haha4" type="text" onblur="alert('触发了onblur事件!')" onpropertychange="alert('触发了onpropertychange事件!')" onchange="alert('触发了   onchange事件!')" size="30" >   <input name="testbutton4" value="经过js改变文本框中的值" type="button" onclick="document.getElementById('haha4').value='js改变文本框后的值'">   <br>   <div>   </body>   </html>

相关文章
相关标签/搜索