为什么Webkit浏览器不触发“beforepaste”事件?

4

beforecopy事件被触发,但beforepaste事件却没有触发。为什么会这样呢?

<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<style type="text/css">#editor{width:300px; height:300px; border: 1px solid black;}</style>
</head>
<body>
<div id="editor" contentEditable="true">editor</div>
<script type="text/javascript">
var elEditor = document.getElementById("editor");

elEditor.addEventListener('beforecopy', function(e){
    console.log('beforecopy');
    e.preventDefault();
    e.stopPropagation();
}); 

elEditor.addEventListener('copy', function(e){
    console.log('copy');
}); 

elEditor.addEventListener('beforepaste', function(e){
    console.log('beforepaste');
    e.preventDefault();
    e.stopPropagation();
}); 

elEditor.addEventListener('paste', function(e){
    console.log('paste')
});
</script>
</body>
</html>
1个回答

8
在Chrome 23.0.1271.95上的OS X 10.8.2中,只有当用户引发上下文菜单时,onbeforepaste事件才会触发。
根据http://help.dottoro.com/ljxqbxkf.php
在剪贴板内容被粘贴到文档之前发生,并提供了启用“粘贴”菜单项的可能性。
右键单击文本框会导致onbeforepaste事件触发,但按下ctrl - v则不会。
如果安慰的话,在我的测试中,onpaste事件在文本实际粘贴之前就会触发,因此我会使用onpaste事件。

除非你想知道被粘贴的文本是什么(也许是为了在粘贴前进行一些预处理),否则不需要这样做。 - saluce
据我所知,onbeforepaste在Safari中与粘贴一起使用更加有用,因为Safari会阻止粘贴操作,除非它是contentEditable。 - Perry Monschau

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接