国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

JavaScript實現復制或剪切內容到剪貼板功能的方法

2019-11-20 09:57:04
字體:
來源:轉載
供稿:網友

項目中需要實現一個點擊按鈕復制鏈接的功能,網上看到的幾款插件,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();    }  };

    
   

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 贡觉县| 祥云县| 自贡市| 汉阴县| 新化县| 固原市| 海南省| 吴忠市| 当阳市| 香港 | 克什克腾旗| 尼玛县| 宁化县| 阿荣旗| 武城县| 浪卡子县| 曲周县| 龙川县| 双桥区| 慈溪市| 北辰区| 都匀市| 呼伦贝尔市| 宁蒗| 海门市| 开阳县| 鹿邑县| 遵化市| 济源市| 游戏| 安西县| 沙坪坝区| 通榆县| 罗定市| 萨迦县| 洛川县| 雅江县| 四子王旗| 神木县| 册亨县| 墨竹工卡县|