A good way to copy a table on a webpage through js script someone asked me today: how to copy a table on a webpage through js script? It seems difficult, but it is still done. By the way, we will record the process of solving this problem and hope it will be helpful to the guys who write the script.
I have never written such a script before, but I just saw a script that can select all the content of the HTML editor. so I came up with a solution: Use the script to insert the table into the HTML editor, then select all the content in the HTML editor and execute the copy command of the HTML editor to copy the table. this will be done soon:
<Input type = "button" value = "copy Test table" onclick = "CopyTable () "> <input type =" button "value =" output the clipboard content to the editor "onclick =" PastClipboardData () "> Test <TABLE border =" 1 "id =" oTable "> <TR> <TD> Test TABLE </TD> </TR> <TR> <TD> Test TABLE </TD> </TR> </TABLE> text <script language = "JavaScript"> <! -- Function CopyTable () {CopyHtmlElement (oTable)} function CopyHtmlElement (obj) {editor.doc ument. designMode = 'on'; // Convert iframe to editable mode, that is, HTML editor editor.doc ument. write ("<body> </body>"); // initialize the editor editor.doc ument. body. innerHTML = obj. outerHTML; editor.doc ument. body. createTextRange (). select (); // select all content in the editor editor.document.exe cCommand ("copy", "", null); // copy} function PastClipboardData () {editor. focus (); editor.document.exe cCommand ("paste", "", null); // paste} // --> SCRIPT
[Ctrl + A Select All Note: If you need to introduce external Js, you need to refresh it to execute]
This function is implemented, but it is a bit uncomfortable. you must use the HTML editor. However, from the code of the all-selected HTML editor, you have noticed the createTextRange () method, this method has been used before, but has not been studied in depth. In Msdn, it was found that only objects such as Body and TextArea support the createTextRange () method. after reading it carefully in msdn, createTextRange () returns a TextRange object and continues to check the TextRange object, I found that there are many methods. I tried the findText method and found that only the text can be selected, but the object cannot be selected. I continued to find it. Finally, I found that moveToElementText is the method we are looking:
<Input type = "button" value = "select Test table" onclick = "CopyTable () "> Test <TABLE border =" 1 "id =" oTable "> <TR> <TD> Test TABLE </TD> </TR> <TR> <TD> Test TABLE </TD> </TR> </TABLE> text <script language = "JavaScript"> <! -- Function CopyTable () {var txt = document. body. createTextRange (); txt. moveToElementText (oTable); txt. select ();} // --> SCRIPT
[Ctrl + A Select All Note: If you need to introduce external Js, you need to refresh it to execute]
Similar to this problem, you can think about whether you have ever written similar code before. if not, you can use a keyword to search for it on the Internet and solve it directly by using existing knowledge. Msdn is a very good treasure. just like the dictionary for Chinese characters in primary schools, you can find the vast majority of required materials. According to that sentence, do it yourself :)