JavaScript實現複製或剪下內容到剪貼簿功能的方法_javascript技巧

來源:互聯網
上載者:User

項目中需要實現一個點擊按鈕複製連結的功能,網上看到的幾款外掛程式,ZeroClipboard是通過flash實現的複製功能,隨著越來越多的提議廢除flash,能不能通過JS來實現複製剪下呢,今天分享一個相容IE7瀏覽器複製的外掛程式給大家,支援使用javascript實現複製、剪下和粘貼。
方法。

複製

var copy = new clipBoard(document.getElementById('data'), {  beforeCopy: function() {  },  copy: function() {    return document.getElementById('data').value;  },  afterCopy: function() {  }});

複製將自動被調用,如果你想要自己調用:

var copy = new clipBoard(document.getElementById('data'));copy.copyd();

document.getElementById('data') :要擷取的對象, 你也可以使用jQuery $('#data')

剪下

基本上與複製的實現方法相同:

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();pastevar 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();    }  };

    
   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.