<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <style type="text/css"> .kcont { border: 1px solid #ccc; background-color: #D2E9FF; position:relative; height:300px; font-size:16px; } .area { width:99%; background:none; border:none; display:block; font-size:16px;} </style> </head> <body> <div class="kcont" id="cont"> <p> 复制我!!!!!!!!</p> function trim(str){ <br /> str = str.replace(/^(s|u00A0)+/,''); <br /> for(var i=str.length-1; i>=0; i--){ <br /> if(/S/.test(str.charAt(i))){ <br /> str = str.substring(0, i+1); <br /> break; <br /> } <br /> } <br /> return str; <br /> } </p> <form id="form"><input type="submit" name="button" id="button" value="复制" /></form> </div> <script> var but = document.getElementById("button"); but.addEventListener("click",copy); Selection = function(input){ this.isTA = (this.input = input).nodeName.toLowerCase() == "textarea"; }; with({o: Selection.prototype}){ o.setCaret = function(start, end){ var o = this.input; if(Selection.isStandard) o.setSelectionRange(start, end); else if(Selection.isSupported){ var t = this.input.createTextRange(); end -= start + o.value.slice(start + 1, end).split(" ").length - 1; start -= o.value.slice(0, start).split(" ").length - 1; t.move("character", start), t.moveEnd("character", end), t.select(); } }; o.getCaret = function(){ var o = this.input, d = document; if(Selection.isStandard) return {start: o.selectionStart, end: o.selectionEnd}; else if(Selection.isSupported){ var s = (this.input.focus(), d.selection.createRange()), r, start, end, value; if(s.parentElement() != o) return {start: 0, end: 0}; if(this.isTA ? (r = s.duplicate()).moveToElementText(o) : r = o.createTextRange(), !this.isTA) return r.setEndPoint("EndToStart", s), {start: r.text.length, end: r.text.length + s.text.length}; for(var $ = "[###]"; (value = o.value).indexOf($) + 1; $ += $); r.setEndPoint("StartToEnd", s), r.text = $ + r.text, end = o.value.indexOf($); s.text = $, start = o.value.indexOf($); if(d.execCommand && d.queryCommandSupported("Undo")) for(r = 3; --r; d.execCommand("Undo")); return o.value = value, this.setCaret(start, end), {start: start, end: end}; } return {start: 0, end: 0}; }; o.getText = function(){ var o = this.getCaret(); return this.input.value.slice(o.start, o.end); }; o.setText = function(text){ var o = this.getCaret(), i = this.input, s = i.value; i.value = s.slice(0, o.start) + text + s.slice(o.end); this.setCaret(o.start += text.length, o.start); }; new function(){ var d = document, o = d.createElement("input"), s = Selection; s.isStandard = "selectionStart" in o; s.isSupported = s.isStandard || (o = d.selection) && !!o.createRange(); }; } function copy (){ var cont = document.getElementById("cont"); var ele = document.createElement("textarea"); ele.style.height = cont.offsetHeight+"px" ele.className = "area"; ele.id = "area"; ele.innerHTML = cont.innerText ; cont.innerHTML = ""; cont.appendChild(ele); var le = document.getElementById("area"); var selection = new Selection(le); selection.setCaret(0,le.value.length); le.focus(); } </script> </body> </html>