就是簡單的點擊拷貝,好像很多網站都有複製代碼這個按鈕。但是好像說很多瀏覽器不支援,要用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');
}
});
Hello I'm some text
Copy Textarea
詳見http://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript