document.execCommand()注意项

2018-10-23  本文已影响0人  欧巴归来不看山

在javascript中document.execCommand()方法是将文字复制到粘贴板,然后复制到其他地方使用。具体的实现方法可以如下:

copyToClipboard(txt = '') {
    let iscopy = false;
    if (document) {
        let textArea = document.createElement('textarea');
        textArea.style.position = 'fixed';
        textArea.style.top = 0;
        textArea.style.left = 0;
        textArea.style.width = '2em';
        textArea.style.height = '2em';
        textArea.style.padding = 0;
        textArea.style.border = 'none';
        textArea.style.outline = 'none';
        textArea.style.boxShadow = 'none';
        textArea.style.background = 'transparent';
        textArea.value = txt;
        document.body.appendChild(textArea);
        textArea.select();

        try {
            document.execCommand('copy');
            iscopy = true;
        } catch (err) {
            console.log('不能使用这种方法复制内容' + err.toString());
        }
        document.body.removeChild(textArea);
    }
    return iscopy;
}

注意

上一篇 下一篇

猜你喜欢

热点阅读