打印本文 打印本文 关闭窗口 关闭窗口
通过JS复制网页表格及数据
作者:武汉SEO闵涛  文章来源:敏韬网  点击数1382  更新时间:2009/6/9 2:31:05  文章录入:mintao  责任编辑:mintao

-------------代码参考一--------------
<INPUT TYPE="button" value="复制测试表格" onclick="CopyTable()">
<INPUT TYPE="button" value="将剪切板内容输出到编辑器中" onclick="PastClipboardData()"><BR>
测试
<TABLE border="1" id="oTable">
<TR>
 <TD>测试表格</TD>
 <TD>测试表格</TD>
</TR>
<TR>
 <TD>测试表格</TD>
 <TD>测试表格</TD>
</TR>
</TABLE>文字<BR><BR>
<iframe id="editor" src="about:blank"></iframe>
<SCRIPT LANGUAGE="JavaScript">
<!--
function CopyTable()
{
 CopyHtmlElement(oTable)
}
 
function CopyHtmlElement(obj)
{
 editor.document.designMode = 'On'; // 将iframe变成可编辑模式,即HTML编辑器
 editor.document.write("<body></body>");  // 初始化编辑器
 editor.document.body.innerHTML = obj.outerHTML;
 editor.document.body.createTextRange().select(); // 选中编辑器内所有内容
 editor.document.execCommand("copy","",null); // 复制
}
function PastClipboardData()
{
 editor.focus();
 editor.document.execCommand("paste","",null); // 粘贴
}
//-->
</SCRIPT>
----------------------代码参考一结束---------------------------

代码一缺点:发现只能选中文字,不能选中对象

----------------------代码参考二--------------------------------
<INPUT TYPE="button" value="选中测试表格" onclick="CopyTable()">
测试
<TABLE border="1" id="oTable">
<TR>
 <TD>测试表格</TD>
 <TD>测试表格</TD>
</TR>
<TR>
 <TD>测试表格</TD>
 <TD>测试表格</TD>
</TR>
</TABLE>文字
<SCRIPT LANGUAGE="JavaScript">
<!--
function CopyTable()
{
 var txt = document.body.createTextRange();
 txt.moveToElementText(oTable);
 txt.select();
}
//-->
</SCRIPT>
---------------------------代码参考二结束---------------------------

打印本文 打印本文 关闭窗口 关闭窗口