如何确定 onChange 事件是否由 onPaste 调用
我有一个与 onChange
事件关联的 Javascript 函数。据我了解,某些浏览器在文档或元素级别支持 onPaste
。有没有办法确定 onChange
事件是否是由“粘贴”引起的 - 我尝试添加一个全局变量,该变量在触发 onPaste
时设置,然后重置它在我的 onChange
例程结束时,但不能保证 onPaste
函数在 onChange
之前被调用。
I have a Javascript function that is associated with an onChange
event. I understand that some browsers support onPaste
at either the document or an element level. Is there a way to determine if the onChange
event was caused by a "paste" - I tried adding a global var that gets set when onPaste
is fired, and then reset it at the end of my onChange
routine, but there is no guarantee that the onPaste
function gets called before the onChange
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这对我来说效果很好:
This worked fine for me:
您可以使用 onKeyPress/onKeyUp/onKeyDown/onPaste 而不是 onChange。
You could use onKeyPress/onKeyUp/onKeyDown/onPaste instead of onChange.
我认为你无法普遍做到这一点。请参阅测试此功能的当前状态。
检测粘贴的一种方法是计算字段获得焦点后“按键”之间的时间。
I don't think you'll be able to do this universally. See the current state of testing this feature.
One way to detect a paste would be to count the time between "keypresses" once a field has focus.
每次看到 onchange 事件时,都会将字段值的当前长度与之前的长度进行比较,并将当前时间与之前的时间进行比较。如果输入字符的速度快于人们可以想象的打字速度,那么它们一定是被粘贴了。
Every time you see an onchange event compare the current length of the value of the field to the previous length, and the current time to the previous time. If the characters were entered faster than a person could conceivably type them, they must have been pasted.