How to paste on click? It works in google docs

前端 未结 3 1396
予麋鹿
予麋鹿 2020-12-02 15:58

I want to be able to initiate real paste event when user clicks. I can understand this may be a security issue, because if any webpage had access to users clipboard, that wo

3条回答
  •  执笔经年
    2020-12-02 16:25

    It works differently with different browsers. You can find a working implementation here: https://jsfiddle.net/1vmansr2/

    Relevant JS:

    function myFunction() {
    
       navigator.clipboard.readText()
    .then(text => {
    document.getElementById("demo").innerHTML = text;
    
    })
    .catch(err => {
    document.getElementById("demo").innerHTML = 'Failed to read clipboard contents: '+err;
    });
    
    
    }
    

提交回复
热议问题