jquery implementation copied to the Clipboard

Source: Internet
Author: User

Some time ago need to do a click a button to copy the design content to the Clipboard effect.

For IE browsers, there is an easy way to pass Window.clipboarddata:

(If there is a button with the ID copy, there is an input box with ID name, "text" is a fixed format)

$ ("#copy"). Click (function () {

Window.clipboardData.setData ("Text", $ ("#name"). Val ());

Alert ("copied to clipboard");

});


But this method is limited to IE browser, other browsers can not use, so do not recommend use.

Here's a way to be compatible with a mainstream browser: jquery's Zclip plugin.

1, first download a Zclip plug-in, the internet has a lot of download resources, casual Baidu, which has two main files: Jquery.zclip.min, zeroclipboard.swf. (Note that two are imported)

2, the two files are put into the project, while referencing JS such as: <script src=. /plugins/js/jquery.zclip.min.js "></script> (my JS files are placed in the root directory under the plugins file under the JS file)


3, the following simulation of the specific code:

<a href= "www.baidu.com" id= "url" >www.baidu.com</a>

Click the "Copy" button:

$ ("#copy"). Zclip ({

<span style= "White-space:pre" >	</span>path: ". /plugins/js/zeroclipboard.swf ",

<span style= "White-space:pre" >	</span>copy:function () {<span style= "White-space:pre" >	

<span style= "White-space:pre" >		</span>return $ ("#url"). Text ();

<span style= "White-space:pre" >	</span>},

<span style= "White-space:pre" >	

<span style= "White-space:pre" >		</span>alert ("copied to clipboard");

<span style= "White-space:pre" >	</span>}

});
<span style= "Font-family:verdana, Arial, Helvetica, Sans-serif; Background-color:rgb (254, 254, 242); " > Where path is zeroclipboard.swf the path position in the project, aftercopy Optional. </span> 

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.