常见Js跨浏览器复制代码
来源:互联网 发布:格式化数据恢复 编辑:程序博客网 时间:2024/05/20 22:29
在Internet Explorer中,使用getData()和clearData ()来复制/清除剪贴板。
在Firefox和Safari,Opera,谷歌浏览器,使用execCommand的“Paste”和“Copy”命令复制内容到剪贴板。但由于浏览器的安全限制,使用execCommand的可能并不生效
为了避免安全限制的一个解决方案是使用Flash剪贴板操作,大家可以试试。
以下列出了几种常见Js复制代码,可跨浏览器复制内容
1、复制专题地址和url地址
<input type="button" name="anniu1" onclick='copyToClipBoard()' value="复制本页title和url"><script language="javascript">function copyToClipBoard() { var clipBoardContent = ""; clipBoardContent += document.title; clipBoardContent += "\r\n"; clipBoardContent += this.location.href; //跨浏览器复制内容 copytext(clipBoardContent);}
2、复制url地址
<input type="button" name="anniu2" onclick='copyUrl()' value="复制URL地址"><script language="javascript">function copyUrl() { var clipBoardContent = this.location.href; copytext(clipBoardContent);}
3、点击文本框时,复制文本框里面的内容
<input onclick="oCopy(this)" value="你好.点击我copy内容!"><script language="javascript">function oCopy(obj) { var clipBoardContent = obj.value; //跨浏览器复制内容 copytext(clipBoardContent);}
4、复制文本框中的内容
<input id=imgurl type=text size=32 value="http://cssteach.com" /><input type=button value="复制文本框中的内容" onclick="CopyUrl(imgurl);" /><script language="javascript">function CopyUrl(obj) { var clipBoardContent = obj.value; //跨浏览器复制内容 copytext(clipBoardContent);}</script>
5、复制span标记中的内容
<span id="tbid">http://cssteach.com/</span>[<a href="#" onclick="copyText(document.all.tbid)">点击复制</a>]<span id="tbid2">http://cssteach.com/1</span>[<a href="#" onclick="copyText(document.all.tbid2)">点击复制</a>]<script type="text/javascript">function copyText(obj) { var clipBoardContent = obj.value; //跨浏览器复制内容 copytext(clipBoardContent);}
核心方法:跨浏览器复制内容
//跨浏览器复制内容function copytext(textToClipboard) { var success = true; if (window.clipboardData) { // Internet Explorer window.clipboardData.setData("Text", textToClipboard); } else { // create a temporary element for the execCommand method var forExecElement = CreateElementForExecCommand(textToClipboard); /* Select the contents of the element (the execCommand for 'copy' method works on the selection) */ SelectContent(forExecElement); var supported = true; //Firefox try { if (window.netscape && netscape.security) { netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); } // 复制 Firefo、Safari success = document.execCommand("copy", false, null); } catch (e) { success = false; } // remove the temporary element document.body.removeChild(forExecElement); } if (success) { alert("文本复制成功,可以粘贴了!"); } else { alert("您的浏览器不允许剪贴板访问!"); }}function CreateElementForExecCommand(textToClipboard) { var forExecElement = document.createElement("div"); // place outside the visible area forExecElement.style.position = "absolute"; forExecElement.style.left = "-10000px"; forExecElement.style.top = "-10000px"; // write the necessary text into the element and append to the document forExecElement.textContent = textToClipboard; document.body.appendChild(forExecElement); // the contentEditable mode is necessary for the execCommand method in Firefox forExecElement.contentEditable = true; return forExecElement;}function SelectContent(element) { // first create a range var rangeToSelect = document.createRange(); rangeToSelect.selectNodeContents(element); // select the contents var selection = window.getSelection(); selection.removeAllRanges(); selection.addRange(rangeToSelect);}
转载至:http://www.cssteach.com/show-10-18-1.html (CSS模板园)
0 0
- 常见Js跨浏览器复制代码
- 常见js复制代码
- 常见Js复制代码
- 常见Js复制代码
- js复制到黏贴板代码 可以兼容多浏览器
- JS代码点击复制给好友,支持IE、火狐浏览器
- JS 实现跨浏览器复制: ZeroClipboard
- 《程序员的第一年》---------- js复制图片与复制文本<待续兼容各浏览器的复制代码..>
- js自动复制代码
- 防止复制 js代码
- js点击复制代码
- js防复制代码
- js复制代码
- Flash+JS实现跨浏览器复制剪贴板功能
- 使用zeroclipboard Js插件,实现跨浏览器复制
- ZeroClipboard.js复制内容到剪贴板(跨浏览器)
- js判断常见浏览器类型
- 一段跨浏览器的"复制到剪贴板"javascript代码
- banner图片自适应
- MySQL数据库引擎介绍、区别、创建和性能测试的深入分析
- 冒泡排序C++
- String类型的 == 和 equals() 判断,及运算符优先级的问题
- C#委托与C++函数指针辨析
- 常见Js跨浏览器复制代码
- 《GLSL渲染语言入门与VBO、VAO使用:绘制一个三角形》的正确版本及源代码
- 指针基础知识 ,破坏指针数组的讨论,
- FZU 1058粗心的物理学家
- 一台电脑启动多个tomcat
- 字段定义引起的bug
- cs231n Lecture 3
- pngquant——一个好用的png压缩工具
- Java中finalize方法