js+html5實(shí)現(xiàn)復(fù)制文字按鈕

本文實(shí)例為大家分享了js+html5實(shí)現(xiàn)點(diǎn)擊復(fù)制文字的按鈕,供大家參考,具體內(nèi)容如下

創(chuàng)新互聯(lián)公司成立于2013年,我們提供高端重慶網(wǎng)站建設(shè)成都網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、網(wǎng)站定制、全網(wǎng)整合營(yíng)銷推廣、微信平臺(tái)小程序開發(fā)、微信公眾號(hào)開發(fā)、網(wǎng)站推廣服務(wù),提供專業(yè)營(yíng)銷思路、內(nèi)容策劃、視覺設(shè)計(jì)、程序開發(fā)來完成項(xiàng)目落地,為邊坡防護(hù)網(wǎng)企業(yè)提供源源不斷的流量和訂單咨詢。

圖片展示:

js+html5實(shí)現(xiàn)復(fù)制文字按鈕

注意css中的樣式,有些頁(yè)面復(fù)制不成功就是沒有添加那一句造成的。

代碼塊

<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="UTF-8">
    <title>Title</title>
  </head>

  <style type="text/css">
    *{
      -webkit-user-select: auto;
    }
  </style>

  <body>
    <span id="content">
      你好,好久不見!
    </span>
    <button id="copyBT">復(fù)制</button>
    <script>
      function copyArticle(event) {
        const range = document.createRange();
        range.selectNode(document.getElementById('content'));

        const selection = window.getSelection();
        if(selection.rangeCount > 0) selection.removeAllRanges();
        selection.addRange(range);
        document.execCommand('copy');
        alert("復(fù)制成功!");
      }

      document.getElementById('copyBT').addEventListener('click', copyArticle, false);
    </script>
  </body>

</html>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

網(wǎng)站名稱:js+html5實(shí)現(xiàn)復(fù)制文字按鈕
標(biāo)題來源:http://muchs.cn/article10/isggdo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、App設(shè)計(jì)、定制開發(fā)、用戶體驗(yàn)企業(yè)建站、關(guān)鍵詞優(yōu)化

廣告

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

h5響應(yīng)式網(wǎng)站建設(shè)