Modify Clipboard content after copy event: JavaScript, jQuery

前端 未结 4 892
感情败类
感情败类 2020-12-15 12:30

My requirement: When user copy some content from my web page, with text some HTML tags and carriage retun also gets copied. I need to modify the copied cont

4条回答
  •  -上瘾入骨i
    2020-12-15 12:47

    The currently accepted answer is overly complicated, and causes weird behavior where a user's selection is removed after copy.

    Here is a much simpler solution:

    document.addEventListener('copy', function(e){
      var text = window.getSelection().toString().replace(/[\n\r]+/g, '');
      e.clipboardData.setData('text/plain', text);
      e.preventDefault();
    });
    

提交回复
热议问题