例子
<script type="text/JavaScript">
function jsCopy(){
var e=document.getElementById("contents");//对象是contents
e.select(); //选择对象
document.execCommand("Copy"); //执行浏览器复制命令
}
</script>
<textarea id="contents" cols="40" rows="5"></textarea>
<br />
<input type="button" onClick="jsCopy();" value="复制" />
js复制无非是以下三步:
1、获取复制的对象:document.getElementById("contents")这句话就是获取ID=“contents”的textarea。
2、全选对象的内容:e.select()
3、用execCommand("")函数将内容复制。
下面来详细介绍execCommand("")函数的参数:
1、格式:document.execCommand(sCommand[,交互方式, 动态参数])
2、参数详解:
002 |
[格式]:document.execCommand(”selectAll”) |
006 |
[格式]:document.execCommand(”open”) |
007 |
[说明]这跟VB等编程设计中的webbrowser控件中的命令有些相似,大家也可依此琢磨琢磨。 |
010 |
[格式]:document.execCommand(”saveAs”) |
014 |
[格式]:document.execCommand(”print”) |
020 |
document.execCommand(”Open”); |
023 |
document.execCommand(”SaveAs”); |
026 |
document.execCommand(”Cut”,” false ”, null ); |
029 |
document.execCommand(”Delete”,” false ”, null ); |
032 |
document.execCommand(”FontName”,” false ”,sFontName); |
035 |
document.execCommand(”FontSize”,” false ”,sSize|iSize); |
038 |
document.execCommand(”ForeColor”,” false ”,sColor); |
041 |
document.execCommand(”2D-Position”,” false ”,” true ”); |
044 |
document.execCommand(”AbsolutePosition”,” false ”,” true ”); |
047 |
document.execCommand(”BackColor”,” false ”,sColor); |
050 |
document.execCommand(”Bold”,” false ”, null ); |
053 |
document.execCommand(”Copy”,” false ”, null ); |
056 |
document.execCommand(”CreateBookmark”,” false ”,sAnchorName); |
059 |
document.execCommand(”CreateLink”,” false ”,sLinkURL); |
062 |
document.execCommand(”FormatBlock”,” false ”,sTagName); |
065 |
document.execCommand(”Open”); |
068 |
document.execCommand(”SaveAs”); |
071 |
document.execCommand(”Cut”,” false ”, null ); |
074 |
document.execCommand(”Delete”,” false ”, null ); |
077 |
document.execCommand(”FontName”,” false ”,sFontName); |
080 |
document.execCommand(”FontSize”,” false ”,sSize|iSize); |
083 |
document.execCommand(”ForeColor”,” false ”,sColor); |
086 |
document.execCommand(”2D-Position”,” false ”,” true ”); |
089 |
document.execCommand(”AbsolutePosition”,” false ”,” true ”); |
092 |
document.execCommand(”BackColor”,” false ”,sColor); |
095 |
document.execCommand(”Bold”,” false ”, null ); |
098 |
document.execCommand(”Copy”,” false ”, null ); |
101 |
document.execCommand(”CreateBookmark”,” false ”,sAnchorName); |
104 |
document.execCommand(”CreateLink”,” false ”,sLinkURL); |
107 |
document.execCommand(”FormatBlock”,” false ”,sTagName); |
注:火狐不支持此方法!
找了好久的一个方法,很管用,网上很多文章一大抄,拿下来根本没法用,他们自己也没有试验过,就写,真是服了