javascript - 兼容主流浏览器前提,如何实现实现剪贴板功能,用js。
本文介绍了javascript - 兼容主流浏览器前提,如何实现实现剪贴板功能,用js。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
PS:初步发现是浏览器兼容问题,只有IE支持clipboardData,那么问题来了,有没有更优的方法?
问题阐述:
global.html里有一个id=biao1的iframe,现欲通过js抽取iframe中div标签的文本内容并复制到剪贴板,本例欲采用clipboardData.setData方法并出现如下报错信息,请大牛指点。
报错信息:
zhiliangbaobiao.js:8 Uncaught TypeError: Cannot read property 'setData' of undefined
at copysql1 (zhiliangbaobiao.js:8)
at HTMLInputElement.onclick (global.html:172)
global.html 主要源码
<iframe id="biao1" src="test00.html" frameborder="0" style="width:100%;height:100%;"></iframe>
<input class="btn btn-success" onclick="copysql1()" value="点击复制代码" type="button">
zhiliangbaobiao 主要源码
var ifr = document.getElementById('biao1');
var ifrwin = ifr.window || ifr.contentWindow;
var temp = ifrwin.document.getElementsByTagName("div").item(0).innerText;
window.clipboardData.setData("text",temp);
解决方案
可以使用 clipboard.js 这个类库(注意浏览器兼容性)或者直接上 Flash。
这篇关于javascript - 兼容主流浏览器前提,如何实现实现剪贴板功能,用js。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文