如何在Javascript中粘贴事件后发出警报? [英] How to alert after paste event in Javascript?

查看:51
本文介绍了如何在Javascript中粘贴事件后发出警报?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的过去事件

I have a simple past event as

document.getElementById('paste_area').addEventListener('paste', function() {
    document.getElementById('notice').innerHTML='Text was successfully pasted!';
    alert('Pasted');
}, true);

可以在此处找到工作示例http://jsfiddle.net/XEQzz/

A working example can be found here http://jsfiddle.net/XEQzz/

警报和通知将在粘贴之前出现。 我该如何延迟粘贴事件实际完成之后的警报操作?

The alert and notice will appear before pasting. How can I delay the alert action to happen after paste event has been actually completed?

推荐答案

您可以将您的警报放入 setTimeout

You could put your alert in a setTimeout.

setTimeout(function() {alert('Pasted');}, 0);

这会将代码延迟到值更新后。

This will delay the code until after the value has updated.

请记住, setTimeout 回调中的 this 的值将与

Just keep in mind that this in the setTimeout callback will have a different value than that in the enclosing environment.

如果您需要引用外部 this ,它将作为元素,然后在变量中引用它。

If you'll need a reference to the outer this, which will be the element, then reference it in a variable...

var self = this;
setTimeout(function() {alert(self.value);}, 0);

或使用 .bind() (大多数支持 addEventListener 的浏览器都支持。较早的Safari不支持。) ...

Or use .bind() (Supported in most browsers that support addEventListener. Older Safari didn't support it.)...

setTimeout(function() {alert(this.value);}.bind(this), 0);

这篇关于如何在Javascript中粘贴事件后发出警报?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆