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