使用clipboard.js怎么實(shí)現(xiàn)一個(gè)復(fù)制功能

今天就跟大家聊聊有關(guān)使用clipboard.js怎么實(shí)現(xiàn)一個(gè)復(fù)制功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都做網(wǎng)站、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿(mǎn)足客戶(hù)于互聯(lián)網(wǎng)時(shí)代的眉縣網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

第一種

//html部分
<input type="text" id="copyValue" />
<button type="button" data-clipboard-target='#copyValue'>復(fù)制</button>
//js部分
var clipboard = new Clipboard('button');
clipboard.on('success',function(e){
 e.clearSelection();
 alert('復(fù)制成功');
 });
clipboard.on('error',function(e){
 e.clearSelection();
 alert('復(fù)制失敗');
 });

說(shuō)明:如果我們使用按鈕復(fù)制的是另一個(gè)元素的內(nèi)容,則我們可以使用這種方法。此時(shí)將按鈕稱(chēng)為觸發(fā)元素,被復(fù)制的元素稱(chēng)為目標(biāo)元素。此時(shí)data-clipboard-target的值為目標(biāo)元素的選擇器,而data-clipboard-target的屬性被設(shè)置在觸發(fā)元素上。new Clipboard()為實(shí)例化對(duì)象,參數(shù)可以是HTML元素,元素選擇器。有success和error兩個(gè)事件可以供我們監(jiān)聽(tīng),實(shí)現(xiàn)自己的邏輯。因?yàn)閺?fù)制完成后,目標(biāo)元素會(huì)處于選中狀態(tài),所以我們需要e.clearSelection()取消目標(biāo)元素的選中狀態(tài)。
優(yōu)點(diǎn):復(fù)制的內(nèi)容可以是動(dòng)態(tài)的,目標(biāo)元素的值發(fā)生變化,復(fù)制的值也發(fā)生變化。

適用場(chǎng)景:復(fù)制內(nèi)容可變,不固定。

第二種

//html部分
<button type="button" data-clipboard-text='復(fù)制內(nèi)容'>復(fù)制</button>
//js部分
new Clipboard('button');

說(shuō)明:data-clipboard-text的值為你要復(fù)制的內(nèi)容。無(wú)目標(biāo)元素,只有觸發(fā)元素。

缺點(diǎn):復(fù)制的內(nèi)容是靜態(tài)的,不變的,提前設(shè)置好的。

適用場(chǎng)景:復(fù)制內(nèi)容固定不變

對(duì)于以上缺點(diǎn),我們可以?xún)?yōu)化如下,使之復(fù)制的內(nèi)容也是動(dòng)態(tài)的。

//html部分
 <input type="text" id="copyValue" />
 <button type="button" id="copy">復(fù)制</button>
//js
$('#copy').on('click', function () {
 var value = $('#copyValue').val();
 $('#copy').attr('data-clipboard-text', value);
 var clipboard = new Clipboard('#copy');
 clipboard.on('success', function (e) {
 alert('復(fù)制成功');
 });
 clipboard.on('error', function (e) {
 alert('復(fù)制失敗');
 });
})

接著這里曬出最常用的幾種方式,以供不時(shí)之需。

function-target

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>function-target</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
  <!-- 1. Define some markup -->
  <button class="btn">Copy</button>
  <div>hello</div>

  <!-- 2. Include library -->
  <script src="../dist/clipboard.min.js"></script>

  <!-- 3. Instantiate clipboard -->
  <script>
  var clipboard = new ClipboardJS('.btn', {
    target: function() {
      return document.querySelector('div');
    }
  });

  clipboard.on('success', function(e) {
    //console.log(e);
    alert('復(fù)制成功!')
  });

  clipboard.on('error', function(e) {
    console.log(e);
  });
  </script>
</body>
</html>

function-text

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>function-text</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
  <!-- 1. Define some markup -->
  <button class="btn">Copy</button>

  <!-- 2. Include library -->
  <script src="../dist/clipboard.min.js"></script>

  <!-- 3. Instantiate clipboard -->
  <script>
  var clipboard = new ClipboardJS('.btn', {
    text: function() {
      return 'to be or not to be';
    }
  });

  clipboard.on('success', function(e) {
    console.log(e);
  });

  clipboard.on('error', function(e) {
    console.log(e);
  });
  </script>
</body>
</html>

target-div

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>target-div</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
  <!-- 1. Define some markup -->
  <span class="copy_content">hello 123</span>
  <button class="btn" data-clipboard-action="copy" data-clipboard-target=".copy_content">Copy</button>

  <!-- 2. Include library -->
  <script src="../dist/clipboard.min.js"></script>

  <!-- 3. Instantiate clipboard -->
  <script>
  var clipboard = new ClipboardJS('.btn');

  clipboard.on('success', function(e) {
    console.log(e);
  });

  clipboard.on('error', function(e) {
    console.log(e);
  });
  </script>
</body>
</html>

target-input

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>target-input</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
  <!-- 1. Define some markup -->
  <input id="foo" type="text" value="hello">
  <button class="btn" data-clipboard-action="copy" data-clipboard-target="#foo">Copy</button>

  <!-- 2. Include library -->
  <script src="../dist/clipboard.min.js"></script>

  <!-- 3. Instantiate clipboard -->
  <script>
  var clipboard = new ClipboardJS('.btn');

  clipboard.on('success', function(e) {
    console.log(e);
  });

  clipboard.on('error', function(e) {
    console.log(e);
  });
  </script>
</body>
</html>

target-textarea

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>target-textarea</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
  <!-- 1. Define some markup -->
  <textarea id="bar">hello</textarea>
  <button class="btn" data-clipboard-action="cut" data-clipboard-target="#bar">Cut</button>

  <!-- 2. Include library -->
  <script src="../dist/clipboard.min.js"></script>

  <!-- 3. Instantiate clipboard -->
  <script>
  var clipboard = new ClipboardJS('.btn');

  clipboard.on('success', function(e) {
    console.log(e);
  });

  clipboard.on('error', function(e) {
    console.log(e);
  });
  </script>
</body>
</html>

看完上述內(nèi)容,你們對(duì)使用clipboard.js怎么實(shí)現(xiàn)一個(gè)復(fù)制功能有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

網(wǎng)頁(yè)名稱(chēng):使用clipboard.js怎么實(shí)現(xiàn)一個(gè)復(fù)制功能
URL標(biāo)題:http://muchs.cn/article26/pihpjg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開(kāi)發(fā)標(biāo)簽優(yōu)化、網(wǎng)站內(nèi)鏈、關(guān)鍵詞優(yōu)化、虛擬主機(jī)、自適應(yīng)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名