JavaScript-asking for a JS copy text problem

Source: Internet
Author: User
is simply click Copy, as if many sites have copy code this button. But it seems that many browsers do not support, to use flash, now this problem is generally how to solve

Reply content:

is simply click Copy, as if many sites have copy code this button. But it seems that many browsers do not support, to use flash, now this problem is generally how to solve

Long ago is a need to introduce flash through the call Flash API to access clipboard, the huge disgusting. Since now abobe oneself do not recommend flash, naturally have method.

https://zenorocha.github.io/clipboard.js/
Depende on Selection and document.execCommand API

(内容测试)(你可以在这里粘帖内容)

He JavaScript Document.execcommand (' copy ') support had grown, see the links below for browser updates:

ie10+ (although this document indicates some the there from ie5.5+).
Google Chrome 43+ (~april 2015)
Mozilla Firefox 41+ (Shipping ~september 2015)
Opera 29+ (based on Chromium, ~april 2015)
Simple Example
var copytextareabtn = Document.queryselector ('. js-textareacopybtn ');

Copytextareabtn.addeventlistener (' click ', Function (event) {
var Copytextarea = Document.queryselector ('. Js-copytextarea ');
Copytextarea.select ();

try {

var successful = document.execCommand('copy');var msg = successful ? 'successful' : 'unsuccessful';console.log('Copying text command was ' + msg);

} catch (Err) {

console.log('Oops, unable to copy');

}
});


Hello I ' m some text


Copy Textarea

See Http://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript

  • 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.