oninput 是 HTML5 的标准事件,对于检测 textarea, input:text, input:password 和 input:search 这几个元素通过用户界面发生的内容变化非常有用,在内容修改后立即被触发,不像 onchange 事件需要失去焦点才触发。
只需要绑定事件即可。
$(
'textarea'
).bind(
'input propertychange'
,
function
() {
$(
'.msg'
).html($(
this
).val().length +
' characters'
);
});