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>
* }" o* |" P" W; R+ ]. [
<html lang="en">
! F' `+ L$ V, J' w0 U
<head>
: B# p4 I0 R3 J) I: x
<meta charset="UTF-8">
% X2 D4 X/ c" e& p* G
<meta name="viewport" content="width=device-width, initial-scale=1">
( t$ f5 p0 O; G4 l( X: T& z
<title>Document</title>
% ^ y/ \( R6 c
</head>
0 d. y+ @, n# }/ _: s, n8 c3 P
<body>
a1 e( v( J/ p& J
<script data-cfhash='f9e31' type="text/javascript">
' T0 C$ _$ w( f0 L5 f
function jsCopy(){
" N) S r: M; D' ]% L/ x# I
var e=document.getElementById("copy-num");//对象是copy-num1
/ Z8 K. Q5 I e) T. [4 H
e.select(); //选择对象
' P, {0 k5 s; T8 z
document.execCommand("Copy"); //执行浏览器复制命令
4 v( Z" n$ x" b4 R
alert("复制成功");
) ~& l4 D6 f6 k1 Y3 }& n
}
5 A2 T7 ]- f/ V/ o
/* <![CDATA[ */
1 O% ~' K( k/ K# e. b) h [
!
9 {/ E6 w! C f' o& c, d4 l$ L% H
function() {
' L% V+ g9 g! W! V2 ]& z
try {
5 I3 B% [2 ^4 V( B
var t = "currentScript" in document ? document.currentScript: function() {
3 p7 ?! Y* |8 m9 ]9 n8 t
for (var t = document.getElementsByTagName("script"), e = t.length; e--;) if (t[e].getAttribute("data-cfhash")) return t[e]
. ]4 ^ ?9 {" {- ^4 w8 i
} ();
. a: S- u3 F5 ^' k$ p& K+ d
if (t && t.previousSibling) {
' p6 M! N' J" A9 h3 y5 q, A
var e, r, n, i, c = t.previousSibling,
& _. U5 i$ x1 h2 z! E4 E& W
a = c.getAttribute("data-cfemail");
3 l4 B2 e7 k- U0 t4 q
if (a) {
! v0 P' z6 L% [* {
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 w8 k, P! @: n) w* s, t
e += String.fromCharCode(i);
% K; c% `: t% \/ \) l! t
e = document.createTextNode(e),
+ |% U- d3 s: S; s* d% B% ?. @
c.parentNode.replaceChild(e, c)
, X9 m/ Y( N* n4 |% q
}
. p7 ]% N# P: E P7 F( O" f& E X
t.parentNode.removeChild(t);
7 D& r9 {+ m" q. i: J/ ], O9 M
}
' f% W/ m$ m- A' @$ ?
} catch(u) {}
/ {( e/ u, I2 J- E" v) A! P& U
} ()
$ w& v, p$ }) _1 b2 n
/* ]]> */
% @$ ]6 [* R. ]) ]
: f$ g; ]# H" |0 {. m) j: J) K
</script>
/ {( n# v6 e2 Y
<div class="nav navbar-top-links navbar-right">
' @+ C/ s8 B Z( O4 \
<li>推广链接:</li>
+ ~4 t4 O, S; l0 Y" L) ? H% Q
1 k+ ?, x( n- R! 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>
9 d+ r* {! d7 i
<li><button onClick="jsCopy()" type="button" class="btn btn-sm">复制</button></li>
% w" a1 Y. ~0 S; n. q
, w8 f1 O- T' J, W
</div>
5 s0 Q* f( H% q" a! p
</body>
/ T# D! n# I( G+ \6 B$ u0 n3 f0 s
</html>
4 i* A6 ^3 E2 f9 i) c: k- L
复制代码
案例二:
0 x4 X2 Q( Q/ ]$ P: _
<html>
. `/ B8 U8 ~& O' r2 ^2 C( K
<head>
4 ?, J9 K7 G" H) [0 }
<meta charset="UTF-8">
# g: H' G# j9 g: c
<meta name="viewport" content="width=device-width, initial-scale=1">
+ q' n* T2 Y- H6 f, k
<title>Zero Clipboard Test</title>
* |, b7 w# \1 k7 `0 Q3 c$ M
</script>
6 }& P# }. `% T# X, V+ W
</head>
$ d, X# |% ?2 o6 m3 H! m% p2 V; n
<body>
% J9 \- I' P4 k
<script type="text/javascript">
8 G3 \* }6 A) ~7 L( n5 ^
function jsCopy(){
8 j4 A8 m8 p$ ?4 h
var e=document.getElementById("contents");//对象是contents
1 @; l7 a- i2 V1 F; O/ d' w7 E# m
e.select(); //选择对象
- k' C+ p* D, a4 Z
tag=document.execCommand("Copy"); //执行浏览器复制命令
& }" ^0 i8 P$ z- f
if(tag){
6 o+ l" @7 F5 R0 _ }5 h
alert('复制内容成功');
6 c) x+ |' H0 O
}
* g5 [' {+ y: b- _/ y
}
( W6 n! W" F; D" B
( \2 S, Z6 ^% _ B& r6 g
</script>
; [& k* A+ q5 h( y# X5 }2 X+ v
<textarea id="contents" cols="40" rows="5"></textarea>
) s- W: j% C4 l* T
<br />
6 m4 y5 b/ H, J! G3 E3 h; p- l% g
<input type="button" onClick="jsCopy();" value="复制" />
7 A3 G* X$ @! p0 ~1 n; w
</body>
2 }" J) h" I# E) ^* D
</html>
6 w( P% X6 O3 b0 F( @6 j0 ?" Y
复制代码
以上都可以执行,亲自测试!
|2 Q2 c, [+ V* t" X" P# I7 }
欢迎光临 cncml手绘网 (http://bbs.cncml.com/)
Powered by Discuz! X3.2