複製到剪貼版
這是一個簡單的 tip,本週我建立了一個常見的「複製到剪貼版」按鈕,我以前不曾做過這樣的功能,我想分享我如何實作它。
它很簡單,比較麻煩的是我們必須加入一個 <input/>
與文字複製到 DOM。我們選擇內容並執行 execCommand 複製指令。
execCommand('copy')
將複製時間被選擇的內容
這個指令目前支援所有最新的瀏覽器,允許我們執行其他系統指令,像是:copy
、cut
、paste
和改變字體顏色、大小等等。
document.querySelector('#input').select();
document.execCommand('copy');
具體呈現請看參考這裡
MEET THE NEW JSTIPS BOOK
You no longer need 10+ years of experience to get your dream job.
Use the 100 answers in this short book to boost your confidence and skills to ace the interviews at your favorite companies like Twitter, Google and Netflix.
GET THE BOOK NOW
MEET THE NEW JSTIPS BOOK
The book to ace the JavaScript Interview.
A short book with 100 answers designed to boost your knowledge and help you ace the technical interview within a few days.
GET THE BOOK NOW