js監聽input輸入框值的實時變化實例


情景:監聽input輸入框值的實時變化實例

解決方法:1.在元素上同時綁定oninput和onporpertychanger事件

實例:<script type="text/JavaScript">

  function watch(){

  consolo.log("in")

</script>

<input type="text"  oninput="watch(event)" onporpertychange="watch(event)" />

2.原生js

<script type= "text/javascript" >
  $( function (){
   if ( "\v" == "v" ){ //true為IE瀏覽器,感興趣的同學可以去搜下,據說是現有最流行的判斷瀏覽器的方法
    document.getElementById( "a" ).attachEvent( "onporpertychange" , function (e){
    console.log( "inputting!!" );
    }
  } else {
    document.getElementById( "a" ).addEventListener( "onporpertychange" , function (e){
    console.log( "inputting!!" );
    }
  }
});
</script>
<input type= "text" id= "a" />
3.使用jQuery綁定事件
<script type= "text/javascript" >
  $( function (){
  $( "#a" ).bind( 'input porpertychange' , function (){
    console.log( "e" );
    });
  });
</script>
<input type= "text" id= "a" />

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM