Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript document.executeCommand(';粘贴';)不在chrome中工作_Javascript_Google Chrome - Fatal编程技术网

Javascript document.executeCommand(';粘贴';)不在chrome中工作

Javascript document.executeCommand(';粘贴';)不在chrome中工作,javascript,google-chrome,Javascript,Google Chrome,我在我的网站中添加了一个鼠标右键上下文菜单,其中包含了剪切、复制、粘贴等功能。我使用了document.executeCommand()。剪切和复制可以工作。但粘贴在chrome中不起作用(它在IE中起作用)。我如何解决这个问题。是的,这不起作用。但这一切并没有失去。您可以从粘贴事件中捕获剪贴板的内容 我在这里写到: 这里有一个github: 这是在IE、FF和Chrome(我已经测试过)中工作的纯javascript 出于安全考虑,这可能不起作用,此线程讨论chrome扩展,需要一些剪贴板

我在我的网站中添加了一个鼠标右键上下文菜单,其中包含了剪切、复制、粘贴等功能。我使用了document.executeCommand()。剪切和复制可以工作。但粘贴在chrome中不起作用(它在IE中起作用)。我如何解决这个问题。

是的,这不起作用。但这一切并没有失去。您可以从粘贴事件中捕获剪贴板的内容

我在这里写到:

这里有一个github:

这是在IE、FF和Chrome(我已经测试过)中工作的纯javascript


出于安全考虑,这可能不起作用,此线程讨论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);
})();