onchange,onpropertychange,oninput键盘输入和js赋值时区别,以及输入密码时显示的是星号

最近项目中需要用到文本框中值改变时触发某个事件,第一反应就是onchange事件,但是测试发现如果是用js赋值的话,是不会触发onchange事件的,后来在网上找了一下,都说是在ie浏览器可以用onpropertychange事件,在火狐中使用oninput事件。说是这两个事件是一个效果,很多人没测试就在那转载。哎……

接下来我把测试过的跟大家分享下。

onchange事件是在键盘输入,失去焦点时候如果值改变(ie和火狐效果一样)。

onchange事件用js赋值时不会触发。

在ie浏览器中(本人是ie8,别的ie版本没测试)。

onpropertychange只要值改变都会触发,无论是键盘输入还是js赋值改变都会触发

oninput在ie中无效。

在火狐浏览器中

onpropertychange在火狐中无效。

oninput键盘输入时,只要值改变就触发(不管是否已经失去焦点),onchange是失去焦点时才会触发。

oninput事件js赋值是不会触发该事件的

文采不行,大家将就看,能理解意思就行。

接下来把测试代码分享给大家。

<input type="text" οnchange="testChange();" /><input type="text" onpropertychange="testOnpropertychange();"/><input type="text" οninput="testOninput();"/><input type="button" value="改变文本框值" οnclick="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" οnkeyup="change(this.value)" /></td>
       <td><input type="button" runat="server"  id="tt" οnclick="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" οnchange="testChange();" /><input type="text" onpropertychange="testOnpropertychange();"/><input type="text" οninput="testOninput();"/><input type="button" value="改变文本框值" οnclick="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" οnkeyup="change(this.value)" /></td>
       <td><input type="button" runat="server"  id="tt" οnclick="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"> 

                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值