+
80
-

为啥document.execCommand("paste")在chrome下不起作用?

请问为啥document.execCommand("paste")在chrome下不起作用?但是document.execCommand("cut")与document.execCommand("copy")是起作用的

高手回复

+
0
-

由于安全方面的考虑,chrome等现代浏览器不支持调用,因为它可能使恶意的js脚本从你的剪贴板读取敏感数据(例如密码等)


但是除了ie,其他三家都是支持copy与cut的操作的。

这是copy与cut的代码

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>

<body>
<p>
<textarea id="textarea">Hello, world!</textarea>
</p>


<p>

<button id="copy">复制内容</button> <button id="cut">剪切内容</button>
</p>
<script>
var selectAndCopy = function() {
// Select text
var cutTextarea = document.querySelector('#textarea');
cutTextarea.select();
// Execute copy
var successful = document.execCommand('copy');
var msg = successful ? 'successful': 'unsuccessful';
console.log('Cutting text command was ' + msg);
};


var copyButton = document.querySelector('#copy');
copyButton.addEventListener('click', function(event) {
selectAndCopy();
});
var cutButton = document.querySelector('#cut');
cutButton.addEventListener('click', function(event) {
Cut();
});
var Cut = function() {
var cutTextarea = document.querySelector('#textarea');
cutTextarea.select();

// Execute copy
var successful = document.execCommand('cut');
var msg = successful ? 'successful': 'unsuccessful';
console.log('paste text command was ' + msg);

}
</script>
</body>

</html>


by ppzhuzhu
我知道答案,我要回答