网络编程 
首页 > 网络编程 > 浏览文章

js复制内容到剪贴板代码,js复制代码的简单实例

(编辑:jimmy 日期: 2025/9/20 浏览:3 次 )

如下所示:

<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、参数详解:

1、〖全选〗命令的实现
[格式]:document.execCommand(”selectAll”)
[说明]将选种网页中的全部内容!
 
2、〖打开〗命令的实现
[格式]:document.execCommand(”open”)
[说明]这跟VB等编程设计中的webbrowser控件中的命令有些相似,大家也可依此琢磨琢磨。
 
3、〖另存为〗命令的实现
[格式]:document.execCommand(”saveAs”)
[说明]将该网页保存到本地盘的其它目录!
 
4、〖打印〗命令的实现
[格式]:document.execCommand(”print”)
[说明]当然,你必须装了打印机!
 
Js代码下面列出的是指令参数及意义

//相当于单击文件中的打开按钮
document.execCommand(”Open”);
 
//将当前页面另存为
document.execCommand(”SaveAs”);
 
//剪贴选中的文字到剪贴板;
document.execCommand(”Cut”,”false”,null);
 
//删除选中的文字;
document.execCommand(”Delete”,”false”,null);
 
//改变选中区域的字体;
document.execCommand(”FontName”,”false”,sFontName);
 
//改变选中区域的字体大小;
document.execCommand(”FontSize”,”false”,sSize|iSize);
 
//设置前景颜色;
document.execCommand(”ForeColor”,”false”,sColor);
 
//使绝对定位的对象可直接拖动;
document.execCommand(”2D-Position”,”false”,”true”);
 
//使对象定位变成绝对定位;
document.execCommand(”AbsolutePosition”,”false”,”true”);
 
//设置背景颜色;
document.execCommand(”BackColor”,”false”,sColor);
 
//使选中区域的文字加粗;
document.execCommand(”Bold”,”false”,null);
 
//复制选中的文字到剪贴板;
document.execCommand(”Copy”,”false”,null);
 
//设置指定锚点为书签;
document.execCommand(”CreateBookmark”,”false”,sAnchorName);
 
//将选中文本变成超连接,若第二个参数为true,会出现参数设置对话框;
document.execCommand(”CreateLink”,”false”,sLinkURL);
 
//设置当前块的标签名;
document.execCommand(”FormatBlock”,”false”,sTagName);
 
//相当于单击文件中的打开按钮
document.execCommand(”Open”);
 
//将当前页面另存为
document.execCommand(”SaveAs”);
 
//剪贴选中的文字到剪贴板;
document.execCommand(”Cut”,”false”,null);
 
//删除选中的文字;
document.execCommand(”Delete”,”false”,null);
 
//改变选中区域的字体;
document.execCommand(”FontName”,”false”,sFontName);
 
//改变选中区域的字体大小;
document.execCommand(”FontSize”,”false”,sSize|iSize);
 
//设置前景颜色;
document.execCommand(”ForeColor”,”false”,sColor);
 
//使绝对定位的对象可直接拖动;
document.execCommand(”2D-Position”,”false”,”true”);
 
//使对象定位变成绝对定位;
document.execCommand(”AbsolutePosition”,”false”,”true”);
 
//设置背景颜色;
document.execCommand(”BackColor”,”false”,sColor);
 
//使选中区域的文字加粗;
document.execCommand(”Bold”,”false”,null);
 
//复制选中的文字到剪贴板;
document.execCommand(”Copy”,”false”,null);
 
//设置指定锚点为书签;
document.execCommand(”CreateBookmark”,”false”,sAnchorName);
 
//将选中文本变成超连接,若第二个参数为true,会出现参数设置对话框;
document.execCommand(”CreateLink”,”false”,sLinkURL);
 
//设置当前块的标签名;
document.execCommand(”FormatBlock”,”false”,sTagName);

注:火狐不支持此方法!

以上就是小编为大家带来的js复制内容到剪贴板代码,js复制代码的简单实例全部内容了,希望大家多多支持~

上一篇:js实现各种复制到剪贴板的方法(分享)
下一篇:微信小程序 定义全局数据、函数复用、模版等详细介绍
一句话新闻
微软与英特尔等合作伙伴联合定义“AI PC”:键盘需配有Copilot物理按键
几个月来,英特尔、微软、AMD和其它厂商都在共同推动“AI PC”的想法,朝着更多的AI功能迈进。在近日,英特尔在台北举行的开发者活动中,也宣布了关于AI PC加速计划、新的PC开发者计划和独立硬件供应商计划。
在此次发布会上,英特尔还发布了全新的全新的酷睿Ultra Meteor Lake NUC开发套件,以及联合微软等合作伙伴联合定义“AI PC”的定义标准。
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网 网站地图 SiteMap