0%

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
39
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>复制文本内容保持预留格式</title>
</head>
<body>

<button id="b2">复制2</button>
<pre class="preText"> ---------------EOF---------------
<p style="color:red;">456</p>

****** CORVARINCE ******
1
1450416.8750 94220.6953 58098.9414 -76054.6328 -232771.8750 212933.2500 -68626.6250 61194.3438
124985.3984 240836.2188 203419.0469 27533.9531 -57570.3281 49936.1172 219656.1406 492165.6250
239934.3281 457249.6250 -106036.7109 94176.5859 -313093.4062 -13598.0986 400218.0000 193047.2969
153492.9219
</pre>
</body>
</html>
<script type="text/javascript" src="jquery-1.10.2.min.js"></script>
<script type="text/javascript">
var text = $('.preText').text();
function copyText(text) {
var tag = document.createElement('textarea');
var elementID = "cp_hgz_input";
tag.setAttribute('id', elementID);
tag.value = text;
// document.getElementById('main-content').appendChild(tag);
document.body.appendChild(tag);
document.getElementById(elementID).select();
document.execCommand('copy');
document.getElementById(elementID).remove();
}
$('#b2').click(function(event) {
copyText(text)
});
</script>

img

转自:https://blog.csdn.net/xxxxxxxxYZ/article/details/94572080