【发布时间】:2018-07-25 22:51:35
【问题描述】:
我正在尝试将网页的所有链接复制到剪贴板。我正在尝试将所有锚标记加入一个字符串,将该字符串放入一个输入字段,然后通过document.execCommand("copy") 复制它,但不知何故document.execCommand("copy") 仅适用于浏览器开发人员工具。我希望它在网页中加载的脚本中工作。请帮助我,在此先感谢。
var
body = document.querySelector("body"),
input = document.createElement("textarea"),
a = document.getElementsByTagName("a"),
list = [],
anchor = document.createElement("a");
for (let i = 0; i < a.length; i++){
list.push(a[i]);
};
list = list.join("\r\n");
input.value = list;
input.setAttribute("readonly", "");
input.style = "position: absolute; left: -9999px;";
body.appendChild(input);
input.select();
document.execCommand('copy');
body.removeChild(input);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>test</title>
</head>
<body>
<a href="http://ali.com">sample link</a>
<script src="script.js"></script>
</body>
</html>
【问题讨论】:
标签: javascript html anchor developer-tools execcommand