欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2016年05月23日 17:17:48   投稿:goldensun  
這篇文章主要介紹了JavaScript實現(xiàn)復(fù)制或剪切內(nèi)容到剪貼板功能的方法,我們平時看到的網(wǎng)頁上很多一鍵復(fù)制功能就是如此實現(xiàn),需要的朋友可以參考下

項目中需要實現(xiàn)一個點擊按鈕復(fù)制鏈接的功能,網(wǎng)上看到的幾款插件,ZeroClipboard是通過flash實現(xiàn)的復(fù)制功能,隨著越來越多的提議廢除flash,能不能通過JS來實現(xiàn)復(fù)制剪切呢,今天分享一個兼容IE7瀏覽器復(fù)制的插件給大家,支持使用javascript實現(xiàn)復(fù)制、剪切和粘貼。
方法。

復(fù)制

var copy = new clipBoard(document.getElementById('data'), {
  beforeCopy: function() {

  },
  copy: function() {
    return document.getElementById('data').value;
  },
  afterCopy: function() {

  }
});

復(fù)制將自動被調(diào)用,如果你想要自己調(diào)用:

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

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

剪切

基本上與復(fù)制的實現(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();
    }
  };

    
   

相關(guān)文章

  • 微信小程序?qū)崿F(xiàn)彈出菜單功能

    微信小程序?qū)崿F(xiàn)彈出菜單功能

    最近做項目需要這樣的需求,當(dāng)用戶點擊標(biāo)簽欄按鈕,向下彈出菜單,再次點擊,收回菜單。接下來通過本文給大家介紹微信小程序?qū)崿F(xiàn)彈出菜單功能,感興趣的朋友一起看看吧
    2018-06-06
  • js中獲取鍵盤按下鍵值event.keyCode、event.charCode和event.which的兼容性詳解

    js中獲取鍵盤按下鍵值event.keyCode、event.charCode和event.which的兼容性詳解

    這篇文章主要給大家介紹了關(guān)于Javascript中獲取鍵盤按下鍵值event.keyCode、event.charCode和event.which的兼容性的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-03-03
  • fullPage.js和CSS3實現(xiàn)全屏滾動效果

    fullPage.js和CSS3實現(xiàn)全屏滾動效果

    這篇文章主要為大家詳細(xì)介紹了fullPage.js和CSS3實現(xiàn)全屏滾動效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • 基于HTML5上使用iScroll實現(xiàn)下拉刷新,上拉加載更多

    基于HTML5上使用iScroll實現(xiàn)下拉刷新,上拉加載更多

    本文主要介紹在HTML5中使用iScroll實現(xiàn)下拉刷新,上拉加載更多數(shù)據(jù)的方法,主要就是寫了兩個自定義函數(shù)pullDownAction和pullUpAction,分別在下拉和上拉的事件中調(diào)用他們。
    2016-05-05
  • JS中style.display和style.visibility的區(qū)別實例說明

    JS中style.display和style.visibility的區(qū)別實例說明

    下面的例子說明了這種區(qū)別:在這個例子中,divContent1和divContent2隱藏的時候用的是style.display=none,這時候,后面的div會向上移動,占據(jù)已經(jīng)隱藏的div的空間。divContent3和divContent4用的是style.visibility=hidden來隱藏,但是其隱藏后仍然占據(jù)原來的空間
    2013-03-03
  • js實現(xiàn)電燈開關(guān)效果

    js實現(xiàn)電燈開關(guān)效果

    這篇文章主要為大家詳細(xì)介紹了js實現(xiàn)電燈開關(guān)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-01-01
  • checkbox全選所涉及到的知識點介紹

    checkbox全選所涉及到的知識點介紹

    checkbox全選涉及到的知識點比如IE里起作用,火狐不起作用,getElementById()與getElementsByName()的區(qū)別等等
    2013-12-12
  • 如何使用JS獲取IE上傳文件路徑(IE7,8)

    如何使用JS獲取IE上傳文件路徑(IE7,8)

    本篇文章是對使用JS獲取IE上傳文件路徑的實現(xiàn)代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-07-07
  • javascript定義函數(shù)的方法

    javascript定義函數(shù)的方法

    javscript中定義和聲明函數(shù)有三種方式:正常方法 構(gòu)造函數(shù) 函數(shù)直接量。
    2010-12-12
  • echarts實現(xiàn)排名柱狀圖的示例代碼

    echarts實現(xiàn)排名柱狀圖的示例代碼

    在ECharts中,可以通過設(shè)置數(shù)據(jù)的順序來控制柱狀圖的排序,本文就介紹了echarts實現(xiàn)排名柱狀圖的示例代碼,具有一定的參考價值,感興趣的可以了解一下
    2023-09-09

最新評論