event.clipboardData.setData in copy event

后端 未结 4 1635
花落未央
花落未央 2020-12-02 22:59

I have looked at many posts but could not find a clear current answer to the following two questions as it seems standards and browser support has been constantly changing.<

4条回答
  •  没有蜡笔的小新
    2020-12-02 23:49

    You can also just turn it into a function that calls its own handler and removes it

    function copyStringToClipboard (string) {
        function handler (event){
            event.clipboardData.setData('text/plain', string);
            event.preventDefault();
            document.removeEventListener('copy', handler, true);
        }
    
        document.addEventListener('copy', handler, true);
        document.execCommand('copy');
    }
    

提交回复
热议问题