JavaScript 无法复制到剪贴板

JavaScript copy to clipboard not working

我的脚本中有一个函数会出错。该功能的目的是使用 onClick 事件从静态面板(不是文本框或输入)复制文本。

未捕获类型错误:copyText.select 不是函数

我想要的是让用户能够单击文本并将其复制到他的剪贴板。

也许您可以提供更好的功能?

https://codepen.io/abooo/pen/jYMMMN?editors=1010

function myFunction() {
  var copyText = document.getElementById("display");
  copyText.select();
  document.execCommand("Copy");
  alert("Copied the text: " + copyText.value);
}

来自 w3schools

select()方法用于select文本域的内容。它不适用于 h1 元素。

这将允许您复制元素的文本。虽然我没有用复杂的布局测试它。

如果您想使用它,请删除警报并提供更好的方式让用户知道内容已被复制。

SAFARI: This does not work on Safari before version 10.0. But as of Safari 10.0 this now works.

function copyText(element) {
  var range, selection, worked;

  if (document.body.createTextRange) {
    range = document.body.createTextRange();
    range.moveToElementText(element);
    range.select();
  } else if (window.getSelection) {
    selection = window.getSelection();        
    range = document.createRange();
    range.selectNodeContents(element);
    selection.removeAllRanges();
    selection.addRange(range);
  }
  
  try {
    document.execCommand('copy');
    alert('text copied');
  }
  catch (err) {
    alert('unable to copy text');
  }
}
<h1 id='display' onClick='copyText(this)'>Text Sample</h1>

如果您想在 <input><textarea> 元素上使用它,请告诉我代码不同。

try/catch 适用于会抛出异常的旧版 Safari。因此,如果您不打算在 10.0 版本之前支持 Safari,则可以将其删除。

你好,所以我调查了一下,因为你没有使用文本输入,你不能只使用 .select() 函数。我从 stack overflow 开发人员 Jason 那里借用了一些关于如何突出显示 javaScript

中的项目的代码 selecting text in an element akin to highlighting with your mouse.

function selectedText(element)(){

var doc = document,
text = doc.getElementById(element)
range, selection;

if(doc.body.createTextRange){ 
 range = document.body.createTextRange();
 range.moveToElementText(text);
 range.select(); 
}
else if(window.getSelection){
  selection = window.getSelection();
  range = document.createRange();
  range.selectNodeContents(text);
  selection.removeAllRanges();
  selection.addRange(range);   
}

return range;

然后我修改为return范围。 然后你所要做的就是

document.onclick = function(e){
  if(e.target.className === 'click'){

      var copytext = SelectText('display');
      document.execCommand("copy");
      alert("Copied the text: " + copytext);
   }
}

这里的关键部分是传给.execCommand() is lower case 'copy'

的字符串

Intervalia 的回答很棒。

对它的小改进,有时单击的元素不是您要复制的元素。
所以我建议你传递你要复制的元素的id。

<button onClick='copyText("display")'> Copy </button>
<h1 id='display'> Text Sample </h1>

然后,在函数的第一行执行

element = document.getElementById(element); 

差别不大,但我认为这样更有用。

更新的解决方案(2020 年)使用新的 Clipboard API writeText method which is supported by most modern browsers (see Can I use 了解更多详情)。

//If you want to copyText from Element
function copyTextFromElement(elementID) {
  let element = document.getElementById(elementID); //select the element
  let elementText = element.textContent; //get the text content from the element
  copyText(elementText); //use the copyText function below
}

//If you only want to put some Text in the Clipboard just use this function
// and pass the string to copied as the argument.
function copyText(text) {
  navigator.clipboard.writeText(text);
}
<div id="mytext">This is some text that needs to be copied</div>
<button onclick="copyTextFromElement('mytext')">Copy</button>