js 實時監聽input中值變化
阿新 • • 發佈:2017-06-16
對象 不同 round 字符 實時 char div har 但是
摘自:http://blog.csdn.net/spy19881201/article/details/25537225
示例:
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>RunJS</title> <script id="jquery_183" type="text/javascript" class="library" src="/js/sandbox/jquery/jquery-1.8.3.min.js"></script> </head> <body> <h1 > 實時監測input中值的變化</h1> <input type="text" id="username" autoComplete=‘off‘> <div id="result"></div> </body> </html>
js 實現:
$(function(){ $(‘#username‘).bind(‘input propertychange‘, function() { $(‘#result‘).html($(this).val().length + ‘ characters‘); }); })
類似於,實現微博的‘還能輸入xxx個字符’
oninput,onpropertychange,onchange的用法:
1、onchange觸發事件必須滿足兩個條件:
a)當前對象屬性改變,並且是由鍵盤或鼠標事件激發的(腳本觸發無效)
b)當前對象失去焦點(onblur);
2、onpropertychange的話,只要當前對象屬性發生改變,都會觸發事件,但是它是IE專屬的;
3、oninput是onpropertychange的非IE瀏覽器版本,支持firefox和opera等瀏覽器,但有一點不同,它綁定於對象時,並非該對象所有屬性改變都能觸發事件,它只在對象value值發生改變時奏效。
js 實時監聽input中值變化