您尚未登录,请登录后浏览更多内容! 登录 | 立即注册

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13922|回复: 0
打印 上一主题 下一主题

[js学习资料] 点击按钮复制到剪贴板 js

[复制链接]
跳转到指定楼层
楼主
发表于 2018-7-31 10:14:54 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. <blockquote><span style="font-family: Arial, Helvetica, sans-serif;">案例一:</span>
复制代码
  1. </pre><pre code_snippet_id="1755225" snippet_file_name="blog_20160711_1_4917192" name="code" class="html"><!DOCTYPE html>
    8 `" ?  R; v  H' d4 k- \# x
  2. <html lang="en"># P  r  \) [6 U9 t3 r( o4 ~3 `
  3. <head>
    , \5 r0 R2 ?1 I) f( h
  4.         <meta charset="UTF-8">
    + A7 P! \& S# {4 X/ n7 j
  5.         <meta name="viewport" content="width=device-width, initial-scale=1">/ e  S& }# D4 r. L  M
  6.         <title>Document</title>) k8 [* S9 @; f$ m/ e
  7. </head>& W4 Z( R. U! V2 I# |
  8. <body>! _+ P9 ^- |; S4 Y
  9.         <script data-cfhash='f9e31' type="text/javascript">
    3 a& z/ w. k  e/ h
  10.                   function jsCopy(){
    5 ~6 w) f2 P* Z8 Q
  11.                     var e=document.getElementById("copy-num");//对象是copy-num1
    9 n& s+ m  |/ F7 ]
  12.                     e.select(); //选择对象8 w% K5 N- b+ q) X2 ~- g
  13.                     document.execCommand("Copy"); //执行浏览器复制命令) _; y6 [7 }9 I6 B, }5 d( y
  14.                     alert("复制成功");
    ) H9 }8 I" m) [% @
  15.                 }0 F8 ^2 q3 y$ G  H8 z, |
  16.                   /* <![CDATA[ */
    : U0 E6 K+ Y9 ~3 x5 l) h
  17.                   !
      Z) o  \. z2 A8 t1 ?, W1 @
  18.                   function() {
    - m/ B0 j# X3 I. ]7 }5 A$ O: H
  19.                     try {. X. p  `+ J/ W; K7 {# V1 i0 z
  20.                       var t = "currentScript" in document ? document.currentScript: function() {
    , y  L. e8 e" h- f3 \& N
  21.                         for (var t = document.getElementsByTagName("script"), e = t.length; e--;) if (t[e].getAttribute("data-cfhash")) return t[e]% N9 h8 c' b1 g* D! B$ G" e# }
  22.                       } ();- r  d, J* v$ Z
  23.                       if (t && t.previousSibling) {3 c& P" R: C; b9 C
  24.                         var e, r, n, i, c = t.previousSibling,* M  V. l% `3 q* t1 A( T! s
  25.                         a = c.getAttribute("data-cfemail");7 C( P3 [( u0 h, A! m" n* B
  26.                         if (a) {
    1 V) E, @* L' I& C
  27.                           for (e = "", r = parseInt(a.substr(0, 2), 16), n = 2; a.length - n; n += 2) i = parseInt(a.substr(n, 2), 16) ^ r,- F- N5 ^; D3 _7 q$ r
  28.                           e += String.fromCharCode(i);
    / J2 U- ^( j- N# g
  29.                           e = document.createTextNode(e),
    ( a- m6 S/ V) b1 E$ \- K& T; S5 c* ~
  30.                           c.parentNode.replaceChild(e, c)
    2 z0 F5 ]! W- z; J. `/ J+ M* f
  31.                         }2 c3 K; Y! S5 B
  32.                         t.parentNode.removeChild(t);, y3 ?1 H! j* _0 d
  33.                       }3 ]& m1 _$ _. Q' ~% M
  34.                     } catch(u) {}% q! O) a. U8 v1 z
  35.                   } ()
    6 h  L1 P+ @/ J1 ~+ {
  36.                   /* ]]> *// h) C9 w$ y8 N( L6 F( B
  37.                   ( D9 S* F$ S5 T$ @- G7 O( K0 W
  38.                 </script>* X' ^# d; `9 e# |/ W- T
  39.                                  <div class="nav navbar-top-links navbar-right">6 V1 ~1 a# R6 X
  40.                   <li>推广链接:</li>
    $ i6 z% y& C) j; }
  41.                  9 ^0 ]. k3 [  I" x6 N. A9 V
  42.                     <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 N8 L/ C: x  Z( @0 T& Q$ f, ?4 _
  43.                     <li><button onClick="jsCopy()" type="button" class="btn btn-sm">复制</button></li>
    8 B% H/ y7 x8 W9 F" e; f4 M- k
  44.                  
    4 Z' l# p) k" F* f7 \
  45.                 </div>) k. @$ P- c9 K# M' D: s& L, k$ x9 x
  46. </body>
    ' f% a( w3 u' x1 `$ ~- F
  47. </html>
    5 ?3 O. @, E( ~3 ?, l; E' x8 V
复制代码
案例二:
( q$ A3 j- k7 k" n
  1. <html>
    1 u% y* P. o$ U0 R
  2. <head>
    5 f8 `5 L9 E4 N8 Y& h: Z% G
  3.         <meta charset="UTF-8">
    ! l. w3 ^8 O0 Z0 F; o& t
  4.         <meta name="viewport" content="width=device-width, initial-scale=1">/ b. m% E( e: w! g, v) B
  5.         <title>Zero Clipboard Test</title>
    " ?. X1 J% I2 W
  6. </script>
    ) K& d, L3 T' z  i( H) Q; S7 u
  7. </head>
    ; G# Q. Y5 r  ?0 u# n4 Z8 Z
  8. <body>
    2 u) E  l" i' y" Y( A
  9.    <script type="text/javascript">
    5 |& j0 a& j% Y- D. a; O; U" O
  10.     function jsCopy(){ 2 I( R8 b* v$ P7 o
  11.         var e=document.getElementById("contents");//对象是contents
    ) @# k+ u+ A( B. Y
  12.         e.select(); //选择对象 " }( S* r# y  s) Z- x% M0 Z
  13.         tag=document.execCommand("Copy"); //执行浏览器复制命令: q" [6 a& v0 D2 M) i1 h( g
  14.         if(tag){6 E1 Q6 K* o4 t2 x2 B1 `
  15.                 alert('复制内容成功');
    , e# Z) d, `! g
  16.         }
    3 F. E3 `& A' X: P/ t! n, x% b0 s. v
  17.     }
    / |0 c  V2 _' ?; ]
  18. ; {4 i6 m& W& f  I% x( `$ L2 C9 f
  19. </script> 8 W/ R7 o3 e, R: Y, {% V+ \
  20. <textarea id="contents" cols="40" rows="5"></textarea>/ \1 Y% S" X& s1 v
  21. <br />
    ' ~. D! U7 n) ~7 I" n0 _
  22. <input type="button" onClick="jsCopy();" value="复制" />5 ]; B& T; G* _' E
  23. </body>
    $ i! w4 Q" t% s9 N8 e+ ]
  24. </html>* ~6 w2 n. h' J
复制代码
以上都可以执行,亲自测试!
/ J" Y8 x7 P, e8 G4 q% J1 y
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-8-4 12:45 , Processed in 0.046564 second(s), 19 queries .

Copyright © 2001-2026 Powered by cncml! X3.2. Theme By cncml!