js复制内容到剪贴板代码,js复制代码的简单实例
js复制内容到剪贴板代码,js复制代码的简单实例
发布时间:2016-12-30 来源:查字典编辑
摘要:如下所示:functionjsCopy(){vare=document.getElementById("contents");//对象是co...

如下所示:

<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" 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复制代码的简单实例全部内容了,希望大家多多支持查字典教程网~

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新Javascript教程学习
热门Javascript教程学习
编程开发子分类