Last active
March 20, 2019 07:16
-
-
Save codcodog/d0dea8884b49ecf6b9ae4a08b0d0833e to your computer and use it in GitHub Desktop.
浏览器复制文本
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title></title> | |
</head> | |
<body> | |
<input id="el" type="text" value="Hello World." /> | |
<button id="copy">复制</button> | |
</body> | |
</html> | |
<script> | |
document.getElementById('copy').addEventListener('click', copyText); | |
function copyText() | |
{ | |
let el = document.getElementById('el'); | |
el.select(); | |
if (document.execCommand('copy')) { | |
document.execCommand('copy'); | |
console.log('复制成功') | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment