Turn js--the value of input or textarea into the pasteboard directly via the button

Source: Internet
Author: User
Tags tagname

The Document.activeelement property is a property of the new Document object in HTML 5, which is used to return the element where the cursor is located. When the cursor does not fall within any element of the page, the property value returns the BODY element.

Setselectionrange (start, end) sets the start and end position of the selected text

The ExecCommand method is to execute a command of the current document, the current selection, or the given range.

Click the button to copy the contents of the TextArea text box
<script type= "Text/javascript" >function copytextareact () {var Ocontent=document.getelementbyid ("content"); o Content.select (); Select Object Document.execcommand ("Copy"); Execute Browser Copy command alert ("Copy complete, paste");} </script><textarea cols= "rows=" "id=" Content "value=" ">this is a paragraph......</textarea> <input type= "button" onclick= "Copytextareact ()" value= "click Copy Code"/>
turn the value of input or textarea into the pasteboard directly via the button
function CopyToClipboard (elem) {//create hidden text element, if it doesn ' t already exist var Targetid = "_hiddenc    Opytext_ "; var Isinput = Elem.tagname = = = = = "INPUT" | |    Elem.tagname = = = "TEXTAREA";    var origselectionstart, Origselectionend;        if (isinput) {///can just use the original source element for the selection and copy target = Elem; Origselectionstart = Elem.selectionstart; SelectionStart the start position of the cursor origselectionend = elem.selectionend; Selectionend the end position of the cursor} else {//must use a temporary form element for the selection and copy target =        document.getElementById (Targetid);            if (!target) {var target = document.createelement ("textarea");            Target.style.position = "absolute";            Target.style.left = " -9999px";            Target.style.top = "0";            Target.id = Targetid;        Document.body.appendChild (target);    } target.textcontent = Elem.textcontent; }//SeleCT the content var currentfocus = document.activeelement;//Gets the active element of the current page Target.focus ();     Target.setselectionrange (0, target.value.length);    Copy the selection var succeed;    try {succeed = Document.execcommand ("copy");    } catch (e) {succeed = false; }//restore original Focus if (Currentfocus && typeof currentfocus.focus = = = "function") {CURRENTFO    Cus.focus (); } if (Isinput) {//restore prior Selection previous selection Elem.setselectionrange (Origselectionstart, Origselec    Tionend);    } else {//clear temporary content target.textcontent = ""; } return succeed;} Call this method directly: CopyToClipboard (document.getElementById ("name"));

Article transferred from http://blog.csdn.net/qq377751971/article/details/65446247

Http://www.cnblogs.com/tylerdonet/p/4533782.html

http://showlike.iteye.com/blog/1073642

Turn js--the value of input or textarea into the pasteboard directly via the button

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.