Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当用户在移动浏览器中复制web内容时,如何自动添加版权? setClipboardText=函数(事件){ 变量htmlData、节点、文本数据; event.cancelable=true; event.preventDefault();//添加此代码将使iOS粘贴为空 node=document.createElement('div'); node.appendChild(window.getSelection().getRangeAt(0.cloneContents()); htmlData='Some copyright'+node.innerHTML+''; textData='Some copyright'+window.getSelection().getRangeAt(0); event.clipboardData.setData('text/html',htmlData); event.clipboardData.setData('text/plain',textData); }; 版权范围=document.getElementById(“内容版权”); copyrightRange.addEventListener('copy',函数(e){ 设置剪贴簿文本(e);_Javascript_Ios_Mobile Safari - Fatal编程技术网

Javascript 当用户在移动浏览器中复制web内容时,如何自动添加版权? setClipboardText=函数(事件){ 变量htmlData、节点、文本数据; event.cancelable=true; event.preventDefault();//添加此代码将使iOS粘贴为空 node=document.createElement('div'); node.appendChild(window.getSelection().getRangeAt(0.cloneContents()); htmlData='Some copyright'+node.innerHTML+''; textData='Some copyright'+window.getSelection().getRangeAt(0); event.clipboardData.setData('text/html',htmlData); event.clipboardData.setData('text/plain',textData); }; 版权范围=document.getElementById(“内容版权”); copyrightRange.addEventListener('copy',函数(e){ 设置剪贴簿文本(e);

Javascript 当用户在移动浏览器中复制web内容时,如何自动添加版权? setClipboardText=函数(事件){ 变量htmlData、节点、文本数据; event.cancelable=true; event.preventDefault();//添加此代码将使iOS粘贴为空 node=document.createElement('div'); node.appendChild(window.getSelection().getRangeAt(0.cloneContents()); htmlData='Some copyright'+node.innerHTML+''; textData='Some copyright'+window.getSelection().getRangeAt(0); event.clipboardData.setData('text/html',htmlData); event.clipboardData.setData('text/plain',textData); }; 版权范围=document.getElementById(“内容版权”); copyrightRange.addEventListener('copy',函数(e){ 设置剪贴簿文本(e);,javascript,ios,mobile-safari,Javascript,Ios,Mobile Safari,上面的代码可以在pc浏览器上运行,但在移动浏览器上不起作用。我已经测试过问题是event.preventDefault()但是如果没有它,我的函数就没有意义了。有人能帮我吗?我不确定这是否有效,因为我自己并没有测试它,但是我想你应该使用return false,它应该可以防止默认的事件行为。你也可以在return false旁边尝试event.stopPropagation() Mozilla开发者网络上有更多信息。我在这一页中困惑的是“属性侦听器”在这句话中的精确定义: 或者,对于属性事件侦听

上面的代码可以在pc浏览器上运行,但在移动浏览器上不起作用。我已经测试过问题是
event.preventDefault()但是如果没有它,我的函数就没有意义了。有人能帮我吗?

我不确定这是否有效,因为我自己并没有测试它,但是我想你应该使用
return false
,它应该可以防止默认的事件行为。你也可以在return false旁边尝试event.stopPropagation()

Mozilla开发者网络上有更多信息。我在这一页中困惑的是“属性侦听器”在这句话中的精确定义:

或者,对于属性事件侦听器,您可以从代码中返回false

这个问题的前两个答案也是最相关的:


请记录,我绝对不喜欢网站弄乱你的剪贴板。我也不喜欢,但现在这是我的工作,我必须这么做。谢谢,但它不起作用。你能用手机浏览器访问www.zhihu.com
,然后把一些超过42个字符的答案复制到备忘录中,你可以找到版权。我没有iPhone:|
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);