js 实时监听input中值变化 矫情吗;* 2022-05-30 01:24 167阅读 0赞 转自:http://blog.csdn.net/spy19881201/article/details/25537225 **\[html\]** [view plain][] [copy][view plain] 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> **\[javascript\]** [view plain][] [copy][view plain] 1. $(function()\{ 2. 3. $('\#username').bind('input propertychange', function() \{ 4. $('\#result').html($(this).val().length + ' characters'); 5. \}); 6. 7. \}) 类似于,实现微博的‘还能输入xxx个字符’ oninput,onpropertychange,onchange的用法 onchange触发事件必须满足两个条件: a)当前对象属性改变,并且是由键盘或鼠标事件激发的(脚本触发无效) b)当前对象失去焦点(onblur); onpropertychange的话,只要当前对象属性发生改变,都会触发事件,但是它是IE专属的; oninput是onpropertychange的非IE浏览器版本,支持firefox和opera等浏览器,但有一点不同,它绑定于对象时,并非该对象所有属性改变都能触发事件,它只在对象value值发生改变时奏效。 [view plain]: http://blog.csdn.net/spy19881201/article/details/25537225#
还没有评论,来说两句吧...