当前位置:Gxlcms > PHP教程 > javascript-求教一个js拷贝文本的问题

javascript-求教一个js拷贝文本的问题

时间:2021-07-01 10:21:17 帮助过:4人阅读

就是简单的点击拷贝,好像很多网站都有这个按钮。但是好像说很多浏览器不支持,要用flash,现在这个问题一般都是怎么解决的

回复内容:

就是简单的点击拷贝,好像很多网站都有这个按钮。但是好像说很多浏览器不支持,要用flash,现在这个问题一般都是怎么解决的

long ago都是需要引入flash通过调用flash的api访问clipboard的,巨恶心..既然现在abobe自己都不推荐flash了,自然有办法。

https://zenorocha.github.io/clipboard.js/
depende on Selection and document.execCommand API




he JavaScript document.execCommand('copy') support has grown, see the links below for browser updates:

IE10+ (although this document indicates some support was there from IE5.5+).
Google Chrome 43+ (~April 2015)
Mozilla Firefox 41+ (shipping ~September 2015)
Opera 29+ (based on Chromium 42, ~April 2015)
Simple Example
var copyTextareaBtn = document.querySelector('.js-textareacopybtn');

copyTextareaBtn.addEventListener('click', function(event) {
var copyTextarea = document.querySelector('.js-copytextarea');
copyTextarea.select();

try {

var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
console.log('Copying text command was ' + msg);

} catch (err) {

console.log('Oops, unable to copy');

}
});





详见http://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript

人气教程排行