document.executeCommand('paste') 在 chrome 中不起作用
document.executeCommand('paste') not working in chrome
我在我的网站中添加了一个鼠标右上下文菜单,其中包含功能(剪切,复制,粘贴)。我使用了document.executeCommand()。剪切和复制可以工作。但是粘贴在 Chrome 中不起作用(它在 IE 中工作)。我该如何解决这个问题。
是的,这不起作用。 但一切都没有丢失。 可以从粘贴事件捕获剪贴板的内容。
我在这里写过:http://blog.dmbcllc.com/cross-browser-javascript-copy-and-paste/
这里有一个github:https://github.com/DaveMBush/CrossBrowserCopyAndPaste
这都是直接的JavaScript,适用于IE,FF和Chrome(我已经测试过)
(function () {
var systemPasteReady = false;
var systemPasteContent;
function copy(target) {
// standard way of copying
var textArea = document.createElement('textarea');
textArea.setAttribute('style','width:1px;border:0;opacity:0;');
document.body.appendChild(textArea);
textArea.value = target.innerHTML;
textArea.select();
document.execCommand('copy');
document.body.removeChild(textArea);
}
var textArea;
function paste(target) {
if (window.clipboardData) {
target.innerText = window.clipboardData.getData('Text');
return;
}
function waitForPaste() {
if (!systemPasteReady) {
setTimeout(waitForPaste, 250);
return;
}
target.innerHTML = systemPasteContent;
systemPasteReady = false;
document.body.removeChild(textArea);
textArea = null;
}
// FireFox requires at least one editable
// element on the screen for the paste event to fire
textArea = document.createElement('textarea');
textArea.setAttribute('style', 'width:1px;border:0;opacity:0;');
document.body.appendChild(textArea);
textArea.select();
waitForPaste();
}
function systemPasteListener(evt) {
systemPasteContent = evt.clipboardData.getData('text/plain');
systemPasteReady = true;
evt.preventDefault();
}
function keyBoardListener(evt) {
if (evt.ctrlKey) {
switch(evt.keyCode) {
case 67: // c
copy(evt.target);
break;
case 86: // v
paste(evt.target);
break;
}
}
}
window.addEventListener('paste',systemPasteListener);
document.getElementById('element1').addEventListener('keydown', keyBoardListener);
document.getElementById('element2').addEventListener('keydown', keyBoardListener);
})();
相关文章:
- XMLHttpRequest在移动设备上的chrome上不起作用
- ng disabled在chrome中不起作用.按钮在角度上未禁用
- 鼠标输入在 YUI 的 chrome 中不起作用
- Javascript的某些部分在Chrome中不起作用,但在其他浏览器中可以完美工作
- HTML5视频链接在Chrome中不起作用
- jQuery克隆函数在chrome中不起作用
- jQuery place attr src在Chrome中不起作用
- chrome中的onmouseover(下拉选项标记)事件不起作用-php,javascript
- .ajax调用在chrome中不起作用
- Javascript/AAJAX在Opera中不起作用,在FF/IE/Chrome中完美工作
- 正在处理在chrome扩展中不起作用的js
- MediaSource API demo不起作用(Chrome)
- Chrome 中表格的 CSV 导出不起作用 - JavaScript/AngularJS
- $.getJSON 在 Chrome 和 Safari 中不起作用
- OnClick 事件在 Chrome 中不起作用
- getUserMedia 在 Chrome v50.0.2661.86 上不起作用
- dataTransfer.set拖放的数据在chrome中不起作用
- 简单的鼠标悬停在 Chrome 中不起作用
- window.close();在Chrome和Firefox中不起作用
- Chrome 扩展程序权限不起作用