js复制标题和链接

问题

常常在写博客和作业时候,需要附上参考链接。
希望可以一键得到标题和链接。

解决方案

普通元素

可以使用findid然后复制
但是标题无法使用

<!DOCTYPE html>
<html>
<head>
<title>My title</title>
</head>
<body>

<p>Click the button to display the title of the document.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
  var copyText = window.location.href;
  var elem = document.createElement("textarea");
  document.body.appendChild(elem);
  elem.value = copyText+document.title;
  elem.select();
  document.execCommand("copy");
  document.body.removeChild(elem);
}
</script>

</body>
</html>

在线js/html/css编辑器

https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_doc_title

参考

https://stackoverflow.com/questions/56099569/copying-document-title-to-clipboard
https://jsfiddle.net/canvasjs/zhho73mg/

https://dev.to/tqbit/how-to-use-javascript-to-copy-text-to-the-clipboard-2hi2
https://www.cnblogs.com/huijieoo/articles/5569990.html

posted @ 2021-05-23 21:42  fengmao31  阅读(159)  评论(0编辑  收藏  举报