20150228今天研究一下了用js監測input框的即時變化,參考網絡上的多篇文章,總結了一下,大概有以下幾種方法
以下都是兼容IE,firefox,chrome常用流行瀏覽器
1、在元素上同時綁定 oninput 和onporpertychanger事件
例:
<script type="text/javascript">
function aa(e){alert("inputting!!");}
</script>
<input type="text" id="a" οninput="aa(event)" onporpertychange="aa(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"/>