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>
# ?: [3 M. B1 f4 D
<html lang="en">
/ s4 O3 C! A) i; I& m% s6 R
<head>
6 O* k5 X! R( Q
<meta charset="UTF-8">
- w2 _ J0 ?/ h
<meta name="viewport" content="width=device-width, initial-scale=1">
; y! S+ d \' G
<title>Document</title>
5 {$ H) S, r1 s/ y
</head>
3 h) x7 y0 S: Y1 s. x( s. W' _
<body>
% G1 {6 o5 @0 q/ m
<script data-cfhash='f9e31' type="text/javascript">
+ N4 i0 W0 m: O( F
function jsCopy(){
# F2 t5 P) i E% P6 V( z$ j
var e=document.getElementById("copy-num");//对象是copy-num1
( h( a/ P' A* G' M" A \
e.select(); //选择对象
- N, g6 w6 @# f
document.execCommand("Copy"); //执行浏览器复制命令
5 @8 I- O/ i" } u0 g# C* F
alert("复制成功");
# t' ~) `* q4 A+ O2 U
}
" _! `) w. D- y2 [* N0 a I& S1 h! @
/* <![CDATA[ */
# U; O `% s4 C% D1 Y
!
0 Z2 F5 G% S. a: y+ L
function() {
$ f j/ i) X( ?' ?1 V. l: J3 A$ l
try {
; H$ q, w, O6 n; l7 j
var t = "currentScript" in document ? document.currentScript: function() {
# |' D- f4 @' b: Q7 B7 N
for (var t = document.getElementsByTagName("script"), e = t.length; e--;) if (t[e].getAttribute("data-cfhash")) return t[e]
; x% p0 E' L2 q! c
} ();
1 C4 X9 j; r" B1 _2 l$ E' M
if (t && t.previousSibling) {
/ K' C) J- E3 }: k0 w, w# s5 q
var e, r, n, i, c = t.previousSibling,
# |% k4 ]" `" b1 f- z* p; t
a = c.getAttribute("data-cfemail");
5 T, _+ H' M1 _, ^- @7 B
if (a) {
1 D! w, B& J% H, [6 Y7 y% ]
for (e = "", r = parseInt(a.substr(0, 2), 16), n = 2; a.length - n; n += 2) i = parseInt(a.substr(n, 2), 16) ^ r,
8 G/ }* c) N M9 c- P, ~; l' {/ z
e += String.fromCharCode(i);
8 ? e' a2 O/ j) k; _- S
e = document.createTextNode(e),
( |' Z1 n, G, s/ Y1 W) M0 C2 L
c.parentNode.replaceChild(e, c)
% C W" {/ |' ]: g
}
; M! `- ~. @( p9 X+ T, Q( {0 I% ]
t.parentNode.removeChild(t);
" I6 s6 O, e# M5 |( \
}
8 e8 g; p2 [+ Z5 ]
} catch(u) {}
& i J7 p, G) c" i G6 W+ `8 c
} ()
/ k1 Z$ s j. T9 M0 G1 w
/* ]]> */
5 N. K) s1 @7 w. x2 L; b5 a
$ S8 j% U% I' l; q. e* f
</script>
0 w/ @# b" z4 J# h( M
<div class="nav navbar-top-links navbar-right">
; W. e- l+ V; ?
<li>推广链接:</li>
* R; ~# ^6 _) g0 Q, d" e
: R9 D. U- t$ O. c8 m
<li><input id="copy-num" class="form-control" type="text" value="" style="max-width:100px;display:inline-block;color: #999; margin-top:12px"></li>
0 {$ t. k6 e8 }- d8 e( `
<li><button onClick="jsCopy()" type="button" class="btn btn-sm">复制</button></li>
/ z& x# r. W+ H) L2 ]
: o$ c' O" |6 N7 D( g' n) @
</div>
, I$ G8 `. f$ R/ [- c% I P, ?
</body>
3 `5 }5 i. m& [* W) y6 a' e) x
</html>
# m" k$ X% ?9 _3 d
复制代码
案例二:
: [- ~0 s) y0 f
<html>
+ _& U8 ^' B& |& [% U ]1 e. P
<head>
. y" o0 _; V7 D: u" p
<meta charset="UTF-8">
( M: t) Y$ K& q
<meta name="viewport" content="width=device-width, initial-scale=1">
6 B0 _) G( q+ J7 a3 H; R: b
<title>Zero Clipboard Test</title>
# k- f' O' H3 D
</script>
3 v5 o X0 E7 O6 E6 B* D( v
</head>
) p0 M6 W! `4 m1 R. t( A; G. M" l
<body>
4 T3 _6 r K# F1 [: X0 Z( o- u
<script type="text/javascript">
; Z* k- k9 a! H0 o
function jsCopy(){
$ L. b0 C0 R, a5 T f% l: T
var e=document.getElementById("contents");//对象是contents
/ [6 g6 P6 K& V1 i- L G
e.select(); //选择对象
" o) P: L" `( ?# x% K
tag=document.execCommand("Copy"); //执行浏览器复制命令
# G$ ~" x' b& y5 G, t. I$ {6 a
if(tag){
2 h! \6 y% l& m. ^
alert('复制内容成功');
& T; d/ ]* P3 G! l/ D9 H5 G/ b
}
7 G9 V, J8 j) T' x, D0 J
}
1 ~+ Q& K( |7 F. G
' a' A/ C9 Q* E3 t& x
</script>
0 Q) t/ b M8 f9 F
<textarea id="contents" cols="40" rows="5"></textarea>
& K9 j) F S1 e/ M7 x
<br />
% n' q) h' j3 X3 s( [
<input type="button" onClick="jsCopy();" value="复制" />
0 h1 q Q G% ]% k# R. f$ q( v( c1 e
</body>
. ~9 T% Q" Q) `; X$ d
</html>
- s6 B' l: l: a3 N. y
复制代码
以上都可以执行,亲自测试!
& c. ~: a5 Y1 Q( G( K
欢迎光临 cncml手绘网 (http://bbs.cncml.com/)
Powered by Discuz! X3.2