onchange,onpropertychange,oninput键盘输入和js赋值时区别,以及输入密码时显示的是星号
2013-06-13 23:37
639 查看
最近项目中需要用到文本框中值改变时触发某个事件,第一反应就是onchange事件,但是测试发现如果是用js赋值的话,是不会触发onchange事件的,后来在网上找了一下,都说是在ie浏览器中可以用onpropertychange事件,在火狐中使用oninput事件。说是这两个事件是一个效果,很多人没测试就在那转载。哎……
接下来我把测试过的跟大家分享下。
onchange事件是在键盘输入,失去焦点时候如果值改变(ie和火狐效果一样)。
onchange事件用js赋值时不会触发。
在ie浏览器中(本人是ie8,别的ie版本没测试)。
onpropertychange只要值改变都会触发,无论是键盘输入还是js赋值改变都会触发
oninput在ie中无效。
在火狐浏览器中
onpropertychange在火狐中无效。
oninput键盘输入时,只要值改变就触发(不管是否已经失去焦点),onchange是失去焦点时才会触发。
oninput事件js赋值是不会触发该事件的
文采不行,大家将就看,能理解意思就行。
接下来把测试代码分享给大家。
接下来我把测试过的跟大家分享下。
onchange事件是在键盘输入,失去焦点时候如果值改变(ie和火狐效果一样)。
onchange事件用js赋值时不会触发。
在ie浏览器中(本人是ie8,别的ie版本没测试)。
onpropertychange只要值改变都会触发,无论是键盘输入还是js赋值改变都会触发
oninput在ie中无效。
在火狐浏览器中
onpropertychange在火狐中无效。
oninput键盘输入时,只要值改变就触发(不管是否已经失去焦点),onchange是失去焦点时才会触发。
oninput事件js赋值是不会触发该事件的
文采不行,大家将就看,能理解意思就行。
接下来把测试代码分享给大家。
<input type="text" onchange="testChange();" /><input type="text" onpropertychange="testOnpropertychange();"/><input type="text" oninput="testOninput();"/><input type="button" value="改变文本框值" onclick="changeText();"> function testChange(){alert('cnange');}function testOnpropertychange(){alert('onpropertychange');}function testOninput(){alert('oninput');}function changeText(){document.getElementById("test1").value='文本框值已经改变';document.getElementById("test2").value='文本框值已经改变';document.getElementById("test3").value='文本框值已经改变';} <script type="text/javascript"> var realValue; function change(value) { var length = value.length; realValue = value; document.getElementById("password").value = ""; for (var i = 0; i < length; i++) { document.getElementById("password").value += "*"; } } function att() { document.getElementById("tt").value = realValue; } </script> </head> <body> <form id="form1" runat="server"> <div> <table> <tr> <td>用户名:</td> <td><asp:TextBox ID="txtyong" runat="server"></asp:TextBox> </td> </tr> <tr> <td>密码:</td> <td><input type="text" id="password" runat="server" onkeyup="change(this.value)" /></td> <td><input type="button" runat="server" id="tt" onclick="att()"/></td> </tr> <tr> <td> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </td> </tr> </table> </div> </form> </body> </html> <input type="text" onchange="testChange();" /><input type="text" onpropertychange="testOnpropertychange();"/><input type="text" oninput="testOninput();"/><input type="button" value="改变文本框值" onclick="changeText();"> function testChange(){alert('cnange');}function testOnpropertychange(){alert('onpropertychange');}function testOninput(){alert('oninput');}function changeText(){document.getElementById("test1").value='文本框值已经改变';document.getElementById("test2").value='文本框值已经改变';document.getElementById("test3").value='文本框值已经改变';} <script type="text/javascript"> var realValue; function change(value) { var length = value.length; realValue = value; document.getElementById("password").value = ""; for (var i = 0; i < length; i++) { document.getElementById("password").value += "*"; } } function att() { document.getElementById("tt").value = realValue; } </script> </head> <body> <form id="form1" runat="server"> <div> <table> <tr> <td>用户名:</td> <td><asp:TextBox ID="txtyong" runat="server"></asp:TextBox> </td> </tr> <tr> <td>密码:</td> <td><input type="text" id="password" runat="server" onkeyup="change(this.value)" /></td> <td><input type="button" runat="server" id="tt" onclick="att()"/></td> </tr> <tr> <td> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </td> </tr> </table> </div> </form> </body> </html>
<pre class="javascript" name="code">
相关文章推荐
- js学习小结(十四)2014.5.23(onpropertychange、oninput、onchange的区别与联系以及正则表达式的应用)
- js脚本给text动态赋值,如何监听:oninput,onpropertychange的区别
- oninput,onpropertychange,onchange的用法和区别
- JS中onpropertychange和onchange事件区别小结
- oninput,onpropertychange,onchange的用法和区别
- js与jquery实时监听输入框值的oninput与onpropertychange方法
- oninput,onpropertychange,onchange的用法和区别
- js与jquery实时监听输入框值的oninput与onpropertychange方法
- oninput,onpropertychange,onchange的用法和区别【转载】
- oninput,onpropertychange,onchange的用法和区别
- oninput,onpropertychange,onchange的用法和区别
- 实时监控文本框输入的信息,JS中的oninput & onpropertychange事件
- oninput,onpropertychange,onchange的用法和区别
- JS中onpropertychange和onchange事件区别小结
- js与jquery实时监听输入框值的oninput与onpropertychange方法
- oninput,onpropertychange,onchange的用法和区别
- oninput,onpropertychange,onchange的用法和区别
- js/jquery 实时监听输入框值变化的完美方案:oninput & onpropertychange
- oninput,onpropertychange,onchange的用法和区别
- oninput,onpropertychange,onchange的用法和区别