总结 input propertychange 监听输入框

常常会有输入框检查的需求,最常见的就是即时搜索。
即时搜索的三个方案:
(1) change事件—触发该事件必须满足两个条件:
  1. 当前对象属性改变,并且是由键盘或鼠标事件激发的(脚步触发无效)
  2. 当前对象失去焦点(onblur)
(2) keypress 就是只能监听键盘事件,鼠标复制粘贴操作无能为力
(3) propertychange(ie) 和 input 事件
  1. input 是标准的浏览器事件,一般应用于input元素,当 input 的 value 发生变化就会触发,无论是键盘输入还是鼠标粘贴的改变都能及时监听到变化。
  2. propertychange 只要当前对象属性发生改变就会触发。(IE专属的)
目前 input & propertychange 仍然是即时搜索比较好的解决方案。
补充 MDN 上对 input event 的描述:
The DOM input event is fired synchronously when the value of an <input> or <textarea> element is changed. (For input elements with type=checkbox or type=radio, the input event does not fire when a user clicks the control, because the value attribute does not change.) Additionally, it fires on contenteditable editors when its contents are changed. In this case, the event target is the editing host element. If there are two or more elements which have contenteditable as true, "editing host" is the nearest ancestor element whose parent isn't editable. Similarly, it's also fired on root element of designMode editors.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值