-
Notifications
You must be signed in to change notification settings - Fork 0
/
first.html
38 lines (33 loc) · 1.06 KB
/
first.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<!DOCTYPE html>
<html>
<head>
<title>copy demo</title>
<style>
html{font-size:30px;}
</style>
</head>
<body>
<p id="selector">copy the text</p>
<button id="copy">复制</button>
<script type="text/javascript">
var aEle = document.querySelector('#copy');
aEle.addEventListener('click', function() {
var copyDOM = document.querySelector('#selector');
var range = document.createRange();
range.selectNode(copyDOM);
window.getSelection().addRange(range);
var successful = document.execCommand('copy');
try {
// Now that we've selected the anchor text, execute the copy command
var msg = successful ? 'successful' : 'unsuccessful';
console.log('Copy email command was ' + msg);
} catch (err) {
console.log('Oops, unable to copy');
}
// Remove the selections - NOTE: Should use
// removeRange(range) when it is supported
window.getSelection().removeAllRanges();
}, false);
</script>
</body>
</html>