<input onclick=copytable () Type=button value= copy Test form > <input onclick=pastclipboarddata () Type=button Output the Clipboard content to the editor > Test <table id=otable border=1> <TBODY> <TR> <TD> Test form </TD> <TD> Test form </TD></TR> <TR> <TD> Test form </TD> <TD> Test form </td></tr></tbody ></TABLE> text <iframe id=editor src= "About:blank" ></IFRAME> <script language=javascript> <!--function copytable () {copyhtmlelement (otable)} function copyhtmlelement (obj) {editor.document.design Mode = ' on '; Turn an IFRAME into an editable mode, the HTML editor editor.document.write ("<body></body>"); Initialize Editor Editor.document.body.innerHTML = obj.outerhtml; Editor.document.body.createTextRange (). Select (); Select all content in the editor editor.document.execCommand ("copy", "", null); copy} function Pastclipboarddata () {editor.focus (); Editor.document.execCommand ("Paste", "", null); Paste}--> </SCRIPT>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
This function is implemented, but feel a bit uncomfortable, you must use the HTML editor, but from the full selection of HTML editor in the code, notice the createTextRange () method, this method has been useful before, but has not been thoroughly studied. Found in MSDN that only objects such as body, textarea, and so on support the createTextRange () method, continue to be looked up in MSDN, createTextRange () returns a TextRange object, continues to consult TextRange object, found that there are many methods, first tried FindText method, found only text, can not select the object, continue to find, finally found Movetoelementtext is the way we are looking for:
<input type= "button" value= "Selected Test form" onclick= "copytable ()" > Test <table border= "1" id= "otable" > <TR> & Lt td> Test form </TD> <TD> Test form </TD> </TR> <TR> <TD> Test form </TD> <TD> Test form ;/td> </TR> </TABLE> text <script language= "JavaScript" > <!--function copytable () {var txt = Document.body.createTextRange (); Txt.movetoelementtext (otable); Txt.select (); }--> </SCRIPT>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]