我有一个简单的过去事件
document.getElementById('paste_area').addEventListener('paste', function() {
document.getElementById('notice').innerHTML='Text was successfully pasted!';
alert('Pasted');
}, true);
可以在此处找到一个工作示例 http://jsfiddle.net/XEQzz/
粘贴前将出现警报和通知。 如何在粘贴事件实际完成后延迟发生警报操作?
最佳答案
您可以将警报放入 setTimeout
中。
setTimeout(function() {alert('Pasted');}, 0);
这将延迟代码,直到值更新之后。
请记住,setTimeout
回调中的 this
的值将与封闭环境中的值不同。
如果您需要对外部 this
(这将是元素)的引用,则在变量中引用它...
var self = this;
setTimeout(function() {alert(self.value);}, 0);
或者使用 .bind()
(大多数支持 addEventListener
的浏览器都支持。旧版 Safari 不支持。).. .
setTimeout(function() {alert(this.value);}.bind(this), 0);
关于javascript - 如何在 Javascript 中的粘贴事件后发出警报?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13895059/