<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body id="lia-body"> <div class="lia-content"> <div class="inputwrapper"> <label><span>姓名:</span><input type="text" name="fullname"></label> <div class="result"></div> </div> </div> <script src="http://apps.bdimg.com/libs/jquery/1.11.3/jquery.min.js"></script> <script> $(function(){ var $inputwrapper = $('#lia-body .lia-content .inputwrapper'); $inputwrapper.find('input').on('input propertychange',function(){ var result = $(this).val(); console.log(result); $inputwrapper.find('.result').html(result); }); }) </script> </body> </html>
onchange觸發事件必須滿足兩個條件:
1)當前對象屬性改變,並且是由鍵盤或鼠標事件激發的(腳本觸發無效)
2)當前對象失去焦點(onblur);
onpropertychange
只要當前對象屬性發生改變,都會觸發事件,但是它是IE專屬的;
oninput是onpropertychange的非IE版本,支持firefox和opera等瀏覽器
但不同的是,它綁定於對象時,並非該對象所有屬性改變都能觸發事件,只有在對象的value值發生改變時才會生效。
這里我們用來監聽input value的改變再好不過了。