event.clipboardData.setData in copy event

后端 未结 4 1628
花落未央
花落未央 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:44

    Bind the element id with copy event and then get the selected text. You could replace or modify the text. Get the clipboard and set the new text. To get the exact formatting you need to set the type as "text/hmtl". You may also bind it to the document instead of element.

     $(ElementId).bind('copy', function(event) {
        var selectedText = window.getSelection().toString(); 
        selectedText = selectedText.replace(/\u200B/g, "");
    
        clipboardData = event.clipboardData || window.clipboardData || event.originalEvent.clipboardData;
        clipboardData.setData('text/html', selectedText);
    
        event.preventDefault();
      });
    

提交回复
热议问题