Heim  >  Artikel  >  Web-Frontend  >  单击复制文字兼容各浏览器的完美解决方案_javascript技巧

单击复制文字兼容各浏览器的完美解决方案_javascript技巧

WBOY
WBOYOriginal
2016-05-16 17:30:001211Durchsuche

单击复制文字的js。找了很久,由于之前没有接触过,完全不知道兼容ie及标准dom浏览器,不仅仅要通过js,而且需要flash的帮忙。clipboard.swf这个在网上都有下载.

ie允许访问剪贴板。window.clipboardData可以访问
但是标准dom。看了一些文章说的都是用flash设置隐藏。但是flash10是不支持。
但是,还有个方法是可行的,固记录一下,以下方法支持标准dom.

复制代码 代码如下:

(function () {
window['Util'] = {
CACHE:{},
getText : function(){
return Util.CACHE.CLIPBOARD_TEXT;
},
successHide : function(){
alert("复制北海365网招聘邮件地址!");
},
Copy : function (pStr,hasReturn, isdo) {
var html = [];
html.push('');
html.push('');
html.push('');
html.push('');
html.push('');
html.push('
');
var yu=document.getElementById('copy').innerHTML;
document.getElementById('copy').innerHTML=yu+html.join('');
Util.CACHE.CLIPBOARD_TEXT = pStr;
}
}
Util.Copy('zhaopin@beihai365.com ');
})();
  
如果要兼容ie。还得加上
复制代码 代码如下:

if (window.clipboardData) {
window.clipboardData.setData("Text",pStr);
Util.successHide();
}
  
最后这样能实现单击复制文字,并且兼容各浏览器。记住。swf的路径一定要对!如果不对是不出现手型的,这是特征。
复制代码 代码如下:

(function () {
window['Util'] = {
CACHE:{},
getText : function(){
return Util.CACHE.CLIPBOARD_TEXT;
},
successHide : function(){
alert("复制北海365网招聘邮件地址!");
},
Copy : function (pStr,hasReturn, isdo) {
var copy=document.getElementById('copy');
if (window.clipboardData) {
copy.onclick=function(){
window.clipboardData.setData("Text",pStr);
Util.successHide();
}
} else {
var html = [];
html.push('');
html.push('');
html.push('');
html.push('');
html.push('');
html.push('
');
var yu=copy.innerHTML;
copy.innerHTML=yu+html.join('');
Util.CACHE.CLIPBOARD_TEXT = pStr;
}
}
}
Util.Copy('zhaopin@beihai365.com ');
})();
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn