JavaScript實(shí)現(xiàn)復(fù)制或剪切內(nèi)容到剪貼板功能的方法

字號(hào):


    這篇文章主要介紹了JavaScript實(shí)現(xiàn)復(fù)制或剪切內(nèi)容到剪貼板功能的方法,我們平時(shí)看到的網(wǎng)頁(yè)上很多一鍵復(fù)制功能就是如此實(shí)現(xiàn),需要的朋友可以參考下
    項(xiàng)目中需要實(shí)現(xiàn)一個(gè)點(diǎn)擊按鈕復(fù)制鏈接的功能,網(wǎng)上看到的幾款插件,ZeroClipboard是通過(guò)flash實(shí)現(xiàn)的復(fù)制功能,隨著越來(lái)越多的提議廢除flash,能不能通過(guò)JS來(lái)實(shí)現(xiàn)復(fù)制剪切呢,今天分享一個(gè)兼容IE7瀏覽器復(fù)制的插件給大家,支持使用javascript實(shí)現(xiàn)復(fù)制、剪切和粘貼。
    方法。
    復(fù)制
    var copy = new clipBoard(document.getElementById('data'), {
      beforeCopy: function() {
      },
      copy: function() {
        return document.getElementById('data').value;
      },
      afterCopy: function() {
      }
    });
    復(fù)制將自動(dòng)被調(diào)用,如果你想要自己調(diào)用:
    var copy = new clipBoard(document.getElementById('data'));
    copy.copyd();
    document.getElementById('data') :要獲取的對(duì)象, 你也可以使用jQuery $('#data')
    剪切
    基本上與復(fù)制的實(shí)現(xiàn)方法相同:
    var cut = new clipBoard(document.getElementById('data'), {
      beforeCut: function() {
      },
      cut: function() {
        return document.getElementById('data').value;
      },
      afterCut: function() {
      }
    });
    或者
    var cut = new clipBoard(document.getElementById('data'));
    cut.cut();
    paste
    var paste = new clipBoard(document.getElementById('data'), {
      beforePaste: function() {
      },
      paste: function() {
        return document.getElementById('data').value;
      },
      afterPaste: function() {
      }
    });
    或者
    var paste = new clipBoard(document.getElementById('data'));
    paste.paste();
    完整代碼:
    (function(name, fun) {
      if (typeof module !== 'undefined' && module.exports) {
        module.exports = fun();
      } else if (typeof define === 'function' && define.amd) {
        define(fun);
      } else {
        this[name] = fun();
      }
    })('clipBoard', function() {
      "use strict";
      function clipBoard(tar, options) {
        this.options = options || {};
        this.tar = tar[0] || tar;
        // if options contain copy, copy will be applied soon
        if (this.options.copy) {
          this.copyd();
        }
        if(this.options.cut) {
         this.cut();
        }
        if(this.options.paste) {
         this.paste();
        }
      }
      clipBoard.prototype.copyd = function(value) {
        // before the copy it will be called, you can check the value or modify the value
        if (this.options.beforeCopy) {
          this.options.beforeCopy();
        }
        // if the options set copy function, the value will be set. then get the paramer value.
        // above all, if the value is null, then will be set the tar of value
        value = value || this.tar.value || this.tar.innerText;
        if (this.options.copy) {
          value = this.options.copy();
        }
        // for modern browser
        if (document.execCommand) {
          var element = document.createElement('SPAN');
          element.textContent = value;
          document.body.appendChild(element);
          if (document.selection) {
            var range = document.body.createTextRange();
            range.moveToElementText(element);
            range.select();
          } else if (window.getSelection) {
            var range = document.createRange();
            range.selectNode(element);
            window.getSelection().removeAllRanges();
            window.getSelection().addRange(range);
          }
          document.execCommand('copy');
          element.remove ? element.remove() : element.removeNode(true);
        }
        // for ie
        if (window.clipboardData) {
          window.clipboardData.setData('text', value);
        }
        // after copy
        if (this.options.afterCopy) {
          this.options.afterCopy();
        }
      };