input框设置onKeypress事件只能输入数字(兼容火狐和IE9)

来源:互联网 发布:明教喵哥捏脸数据 编辑:程序博客网 时间:2024/06/05 11:40

https://segmentfault.com/q/1010000008818447

使用onInput()事件

oninput 是 HTML5 的标准事件,对于检测 textarea, input:text, input:password 和 input:search 这几个元素通过用户界面发生的内容变化非常有用,在内容修改后立即被触发,不像 onchange 事件需要失去焦点才触发。oninput 事件在主流浏览器的兼容情况如下:

http://runjs.cn/detail/pfojehd8

若使用onKeypress事件在火狐上达不到这个效果,还是会有一个字母可以正常输入。

<!DOCTYPE html><html>    <head>        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">        <title>RunJS</title>    </head>    <body>        <input type="text" onInput ="keypress(this)" />    </body>    </body></html>// scriptsfunction keypress(_this){    _this.value = _this.value.replace(/[^0-9]/g, '');}



阅读全文
0 0
原创粉丝点击