4
setClipboardText = function(event) {
  var htmlData, node, textData;
  event.cancelable = true;
  event.preventDefault(); //add this code will make iOS paste null
  node = document.createElement('div');
  node.appendChild(window.getSelection().getRangeAt(0).cloneContents());
  htmlData = '<div>Some copyright' + node.innerHTML + '</div>';
  textData = 'Some copyright' + window.getSelection().getRangeAt(0);

  event.clipboardData.setData('text/html', htmlData);
  event.clipboardData.setData('text/plain', textData);
};

copyrightRange = document.getElementById('content-copyright');

copyrightRange.addEventListener('copy', function(e) {
  setClipboardText(e);

The code above will work on the pc browsers, but on the mobile browsers, it doesn't work. I have test the problem is event.preventDefault();.But without this, my function will not make sense.Can somebody help me?

EricKK
  • 45
  • 5

1 Answers1

0

I'm not sure if this will work, since I'm not testing this myself, but I think you want to use return false, which is supposed to prevent the default event behaviour. You might also try event.stopPropagation() alongside return false.

There is more info at the Mozilla Developer Network https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XUL/Tutorial/More_Event_Handlers#Prevent_Default_Action. What I'm confused about in this page is the precise definition of an "attribute listener" in this quote:

Alternatively, for attribute event listeners, you can just return false from the code.

This question's top two answers are also mostly relevant:

event.preventDefault() vs. return false

Community
  • 1
  • 1
Devin Howard
  • 675
  • 1
  • 6
  • 17