cncml手绘网
标题:
点击按钮复制到剪贴板 js
[打印本页]
作者:
admin
时间:
2018-7-31 10:14
标题:
点击按钮复制到剪贴板 js
<blockquote><span style="font-family: Arial, Helvetica, sans-serif;">案例一:</span>
复制代码
</pre><pre code_snippet_id="1755225" snippet_file_name="blog_20160711_1_4917192" name="code" class="html"><!DOCTYPE html>
4 S0 U- _; b: O; K
<html lang="en">
+ c' u! ?; W2 h
<head>
) T/ k' H9 K' v" [$ N
<meta charset="UTF-8">
* F: O4 k9 t- u9 T; e% N n
<meta name="viewport" content="width=device-width, initial-scale=1">
- ]4 e, F% @0 w; z
<title>Document</title>
2 \6 e8 O/ a- h1 {& C0 H1 i
</head>
2 G( j5 f6 U- k- V1 u( l
<body>
* N, T: U3 M8 B# w9 E# G: `
<script data-cfhash='f9e31' type="text/javascript">
$ B' g9 A [6 P9 t
function jsCopy(){
# q6 u) p/ ]; l2 h3 w3 q3 \: @+ ~2 X+ J
var e=document.getElementById("copy-num");//对象是copy-num1
' G0 H9 b3 a4 l) v/ B; h! o
e.select(); //选择对象
4 [* Z% M& J* E0 W
document.execCommand("Copy"); //执行浏览器复制命令
& I. h" Y5 r; K) w Z; \) i. J w4 O
alert("复制成功");
8 U" s0 N. g1 J2 S
}
2 n" P2 a/ u0 x. E6 {
/* <![CDATA[ */
7 ]" L/ E' K: r
!
+ ]: J) }* C& U7 S* g
function() {
6 o! M4 V' g. C3 q9 I
try {
6 A* C9 k7 n" t) R! f& S' R0 L
var t = "currentScript" in document ? document.currentScript: function() {
' M, s8 u) ]$ s* G8 @
for (var t = document.getElementsByTagName("script"), e = t.length; e--;) if (t[e].getAttribute("data-cfhash")) return t[e]
6 q/ x$ p8 O1 B. u" R8 A
} ();
, l/ i( E" T4 ^3 `! p
if (t && t.previousSibling) {
: h& V U% b h9 Q; M
var e, r, n, i, c = t.previousSibling,
( k4 c! n9 `- T$ `4 D* g }
a = c.getAttribute("data-cfemail");
/ j3 o4 u$ X7 \6 g
if (a) {
+ h7 @6 ]( `6 r& I2 M" h( f2 _
for (e = "", r = parseInt(a.substr(0, 2), 16), n = 2; a.length - n; n += 2) i = parseInt(a.substr(n, 2), 16) ^ r,
) _% {2 X0 V8 U3 \
e += String.fromCharCode(i);
6 _# ~7 _9 w; F; R( g5 i
e = document.createTextNode(e),
) {1 k3 Q% b/ L i0 N- H
c.parentNode.replaceChild(e, c)
5 i& f/ Y; P9 L5 ~
}
7 [) _0 q- x& E6 q r2 c
t.parentNode.removeChild(t);
- S2 ^9 W1 R1 n! E; {" q
}
8 H6 j _9 X) z; l* X& T
} catch(u) {}
0 i/ E3 n9 w. ?( f h+ c$ \" @
} ()
# f/ C8 Q$ Z1 d% \; q+ |5 B' \4 I
/* ]]> */
% Y- U' d2 ^( [7 A8 `* u
- C+ G$ T( w1 ]4 G/ M ^
</script>
9 x& Q& u8 g8 n" W$ d s% a7 t
<div class="nav navbar-top-links navbar-right">
6 ?6 B+ M. C' p
<li>推广链接:</li>
7 V: ]+ V( r: ?6 u& V* K# g% W8 P4 L
* \3 B8 @2 {, c0 j; @5 [
<li><input id="copy-num" class="form-control" type="text" value="" style="max-width:100px;display:inline-block;color: #999; margin-top:12px"></li>
: Y3 i: O& ?" e) z' F. h% R s' c
<li><button onClick="jsCopy()" type="button" class="btn btn-sm">复制</button></li>
& R: U7 t4 x# t5 v* X
4 h1 y: Y0 }4 _1 G8 ^ {7 q
</div>
. \: E; e% t; b9 e" | V
</body>
( e) u. L- L1 p" q
</html>
7 ~! R5 t, s* Y: m% w8 h
复制代码
案例二:
6 |7 o1 x$ e+ v* ~9 A) L
<html>
6 E# \0 K" K) P$ h& t
<head>
9 g& u: {: F+ `* c7 x
<meta charset="UTF-8">
! P) F" s8 @% ~, d, h
<meta name="viewport" content="width=device-width, initial-scale=1">
4 \; F* u" I( x- R0 k* j
<title>Zero Clipboard Test</title>
8 {: ]7 I; B7 {7 V5 n- E0 V
</script>
6 i1 D7 e! I @$ g% P: e
</head>
+ L% O8 m( U/ N+ {4 H, F
<body>
2 O& J& C0 U& _% ?3 q5 l( i
<script type="text/javascript">
+ w' r! F, f1 f! w, M
function jsCopy(){
( w6 R- C5 Z+ R3 N W7 |; {' s
var e=document.getElementById("contents");//对象是contents
; H" M, O0 b* n; L, z8 [; p0 B/ q0 a
e.select(); //选择对象
p! |5 D+ _$ v. P' z, D
tag=document.execCommand("Copy"); //执行浏览器复制命令
/ {% `% n9 q% H/ C. U% y) X8 s( u
if(tag){
7 o: y; ]. q# j+ [" i
alert('复制内容成功');
1 d; |5 @; d q$ `) B& P
}
% M( Y8 R2 d: j' C% R- D, Y) E" @
}
2 ` D: o g+ R! L+ O- p- M6 D
) A) X$ J V/ Q0 v' F o1 _
</script>
) w0 D0 ^6 `, j6 {2 G
<textarea id="contents" cols="40" rows="5"></textarea>
2 Q/ R2 n* ~) w3 L! R* r) E
<br />
0 @6 K, n2 ^- t8 c1 r! ]+ N, d9 y! R8 K9 R
<input type="button" onClick="jsCopy();" value="复制" />
+ @" s& z6 c. I) M3 e+ A3 e( D- T
</body>
4 f- X, w; T8 f+ W; }0 p" z
</html>
& K z% d. J _! I
复制代码
以上都可以执行,亲自测试!
; {2 _$ |, s* w9 M- T
欢迎光临 cncml手绘网 (http://bbs.cncml.com/)
Powered by Discuz! X3.2