极客时间返利平台,你可以在上边通过山月的链接购买课程,并添加我的微信 (shanyue94) 领取返现。
山月训练营之面试直通车 服务上线了,从准备简历、八股文准备、项目经历准备、面试、面经、面经解答、主观问题答复、谈薪再到入职的一条龙服务。

# input 中监听值的变化是在监听什么事件

Issue

欢迎在 Gtihub Issue 中回答此问题: Issue 215 (opens new window)

可以实时监听值的变化的事件有以下几种

  • keypress
  • keydown
  • keyup
  • input

注: onChange 无法做到实时监听,因为 onChange 需要失去焦点才能触发

oninput 不用考虑是否失去焦点,不管 js 操作还是键盘鼠标手动操作,只要 HTML 元素属性发生改变即可立即捕获到

Author

回答者: qixMan (opens new window)

onchange:

Supported HTML tags:

<input type="checkbox">, <input type="file">, <input type="password">, <input type="radio">, <input type="range">, <input type="search">, <input type="text">, <select> and <textarea>

The onchange attribute fires the moment when the value of the element is changed.

Tip: This event is similar to the oninput event. The difference is that the oninput event occurs immediately after the value of an element has changed, while onchange occurs when the element loses focus. The other difference is that the onchange event also works on

Author

回答者: imondo (opens new window)

@CaiyueDamowang 测试了好像不行

Author

回答者: cy-98 (opens new window)

@CaiyueDamowang 测试了好像不行

嗯,不行。。 在 html 标签中 onchange 可以在失去焦点触发

低版本浏览器 function onPropertyChange(event){console.log(event.srcElement.value)}

ie10 以上 function onInput(event){console.log(event.srcElement.value)}

input 事件

oninput 实时触发,onchange 失去焦点时触发

Last Updated: 9/27/2022, 2:39:59 PM