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