Bright Leopold

i come from the other world,i will go back after the love,the regret,the alive and the dead are over

导航

onpropertychange事件


 昨天因为项目的需要,写了一段js,但是却不知道用什么事件来触发它.
        最后才知道可以用onpropertychange事件,她和onchange有些类似,但是也有不同!
        IE下,当一个HTML元素的属性改变的时候,都能通过 onpropertychange来捕获。例如一个
<input name="text1" id="text1" />
对象的value属性被页面的脚本修改的时候,onchange无法捕获到,而onpropertychange却能够捕获。
也就是说:onpropertychange能及时捕获属性值的变化,而onchange在属性值改变时还必须使得当前元素失去焦点(onblur)才可以激活该事件!
如:
例1:
请输入图片地址: <input type="text" name="mytext" size="10" value="" onpropertychange="document.images['myimg'].src=this.value;" /> <img id="myimg" src="/img/common/logo.gif" alt="" />
当text框中的内容被改变时,图片就会立刻被显示出来。而如果用onchange时,改变其值时还需用鼠标单击空白或其他地方使input元素失去焦点(onblur)才能激活该事件,图片显示才会被改变!
例2:
<INPUT id="image" style="WIDTH: 448px; HEIGHT: 22px"     onpropertychange="preview.src=image.value" type="file" size="55" name="File1" runat="server">
<IMG id="preview" height="170" alt="" src="" width="256">
----------------------------------------------------------------------------------------------------------------------
onpropertychange事件太可爱了,我对它一见钟情
onChange:当前元素失去焦点并且元素的内容发生改变而触发的事件 [鼠标与键盘的触发均可]
所以说当对象的value被脚本改变时不会触发onChange事件,因为用户即没有动鼠标又没动键盘.
本文出自 51CTO.COM技术博客
 
PS:firefox 不支持onpropertychange

原文地址:http://www.phplover.cn/post/js-onchange-onpropertychange.html

posted on 2010-07-15 10:07  Bright Leopold  阅读(446)  评论(1编辑  收藏  举报