情景:監聽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"
/>