2012-12-07 68 views
3

beforecopy事件被觸發,但事件之前沒有觸發事件。這是爲什麼?爲什麼'beforepaste'事件不會在webkit中觸發?

<!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> 

回答

6

在Chrome 23.0.1271.95在OS X 10.8.2,onbeforepaste當用戶使上下文只觸發菜單彈出。

http://help.dottoro.com/ljxqbxkf.php

剪貼板中的內容粘貼到文檔中之前,並提供了可能性,以使粘貼菜單項發生。

右鍵單擊文本框使onbeforepaste事件觸發,但不按CTRL - v

如果它是任何安慰,onpaste事件會在文本實際粘貼之前觸發,在我的測試中,所以我只需使用onpaste事件。

+0

當然,除非你想知道粘貼的文字是什麼(也許在粘貼之前執行一些預處理) – saluce

+0

據我所知,在safari中粘貼之前,先前的粘貼變得更有用,因爲safari會防止粘貼發生,除非它是可編輯的內容。 –

相關問題