JavaScript实现复制或剪切内容到剪贴板功能的方法

作者:代码拾荒

时间:2026-10-31

浏览:147

本文介绍一种兼容IE7浏览器的JavaScript剪贴板操作方案。通过clipBoard插件,开发者可以轻松实现复制、剪切及粘贴功能。文章详细展示了如何初始化插件、配置回调函数以及调用相关方法,并提供完整代码示例,帮助解决Flash废除后的跨浏览器兼容性问题。

文章目录

背景与插件介绍

在项目开发中,经常需要实现点击按钮即可复制链接的功能。早期网络上流行的ZeroClipboard插件依赖Flash技术,但随着Flash逐渐被废弃,寻找纯JavaScript实现的替代方案变得尤为重要。本文介绍一款兼容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();
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();
    }
  };

    
   

实用工具推荐

电子包浆

绿化、压缩、噪点和虚拟水印,自由调节做旧程度。

使用 ›
图片压缩

选择画质和输出格式,预览压缩前后的大小。

使用 ›
相关文章