JavaScript implements ways to copy or cut content to the Clipboard function _javascript tips

Source: Internet
Author: User

The project needs to implement a Click button Copy link function, the Internet to see several plug-ins, Zeroclipboard is implemented through the Flash copy function, as more and more proposed to abolish flash, can be achieved through JS replication cut, Today sharing a compatible IE7 browser copy plugin for everyone, support the use of JavaScript to achieve copy, cut and paste.
Method.

Copy

var copy = new ClipBoard (document.getElementById (' data '), {
  beforecopy:function () {

  },
  copy:function () { C5/>return document.getElementById (' data '). Value;
  },
  aftercopy:function () {

  }
});

Replication will automatically be invoked if you want to call yourself:

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

document.getElementById (' data '): To get the object, you can also use jquery $ (' #data ')

Shear

Basically the same way as the replication implementation:

var cut = new ClipBoard (document.getElementById (' data '), {
  beforecut:function () {

  },
  cut:function () { return
    document.getElementById (' data '). Value;
  },
  aftercut:function () {

  }
});

Or

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 () {

  }
});

Or

var paste = new ClipBoard (document.getElementById (' data '));
Paste.paste ();

Complete code:

(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 would 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'll be called, can check the value O
    R Modify the value if (this.options.beforeCopy) {this.options.beforeCopy (); }//If the options set copy function, the value would be set.
    Then get the Paramer value. Above all, if the value is null, then'll 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 ();

 }
  };

    
   

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.