input实时监听

时间:2023-03-09 21:53:43
input实时监听
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <title>RunJS</title>
  6. <script id="jquery_183" type="text/javascript" class="library" src="/js/sandbox/jquery/jquery-1.8.3.min.js"></script>
  7. </head>
  8. <body>
  9. <h1 >
  10. 实时监测input中值的变化
  11. </h1>
  12. <input type="text" id="username" autoComplete='off'>
  13. <div id="result"></div>
  14. </body>
  15. </html>
  1. $(function(){
  2. $('#username').bind('input propertychange', function() {
  3. $('#result').html($(this).val().length + ' characters');
  4. });
  5. })
类似于,实现微博的‘还能输入xxx个字符’
oninput,onpropertychange,onchange的用法
onchange触发事件必须满足两个条件:
a)当前对象属性改变,并且是由键盘或鼠标事件激发的(脚本触发无效)
b)当前对象失去焦点(onblur);
onpropertychange的话,只要当前对象属性发生改变,都会触发事件,但是它是IE专属的;
oninput是onpropertychange的非IE浏览器版本,支持firefox和opera等浏览器,但有一点不同,它绑定于对象时,并非该对象所有属性改变都能触发事件,它只在对象value值发生改变时奏效。
转自 http://blog.****.net/spy19881201/article/details/25537225