为什么 jQuery 中的粘贴事件会在预粘贴时触发?
我正在尝试使文本框类似于 Twitter,为此我编写了代码:
- 字数
- 使用事件更改、Keyup 和粘贴
Keyup
和 Change 事件工作正常,但粘贴事件有点奇怪,当我在文本区域中粘贴某些内容时,字数此时不会改变,经过一些调试后,我发现粘贴事件在将某些内容粘贴到文本框之前< /强>。我不知道 Twitter 上他们是如何处理这个问题的。
这是我的代码:
事件:
'click #textboxId' : 'submitQuestion'
'keyup #textboxId' : 'wordCounter'
'change #textboxId' : 'wordCounter'
'paste #textboxId' : 'wordCounter'
wordCounter: ->
#Code for Word Count#
由于粘贴事件的预粘贴性质,该实例上的工作计数不会改变。
我们将不胜感激您的建议和帮助。
I am trying to make textbox similar to the Twitter, for this I have written code for:
- Word Count
- Used Events Change, Keyup and Paste
Keyup
and Change
Events are working fine but paste event is little bit strange, when I paste something in textarea the word count doesn't change at that moment, after some debugging I found that paste event fires up before pasting something on textbox. I don't know how they handle this in Twitter.
Here is my code:
events:
'click #textboxId' : 'submitQuestion'
'keyup #textboxId' : 'wordCounter'
'change #textboxId' : 'wordCounter'
'paste #textboxId' : 'wordCounter'
wordCounter: ->
#Code for Word Count#
Due to pre-paste nature of paste event the work count doesn't changes on that instance.
Your suggestion and help will be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
请参阅此示例。
http://jsfiddle.net/urEhK/1
这种行为非常奇怪。您认为其中一个事件能够正确捕捉到这一点吗?我很惊讶谷歌没有找到更多答案。
See this example.
http://jsfiddle.net/urEhK/1
That behavior was very weird. You would think that one of those events would catch this properly? I was surprised there weren't more answers to this via Google.
您现在应该使用 on() 而不是 bind() 查看这篇文章。
也不需要创建命名函数,您可以只创建匿名函数。
You should now use on() instead of bind() see this post.
It's also unnecessary to create a named function, you can just create a anonymous function.
您可以做一件事,首先获取原始数据。然后您可以获取事件粘贴数据并添加它们。
You can do one thing that firstly get original data . then you can get the event paste data and add them.