js 复制文本

152 阅读1分钟
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<title></title>
	</head>
	<body>

	</body>
	<script type="text/javascript">
		// 复制文本
		handleCopy(content: string) {
			// navigator.clipboard  API复制文本
			navigator.clipboard.writeText(content).then(
				() => {
					this.$message.success('复制成功')
				},
				() => {
					/* failure */
				}
			)
		}

		// 复制链接
		handleCopy1(content: string) {
			const ele = document.createElement('input')
			ele.setAttribute('value', content)
			document.body.appendChild(ele)
			ele.select()
			document.execCommand('Copy')
			document.body.removeChild(ele)
		}
	</script>
</html>