forked from loverajoel/jstips
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d42c694
commit 9b6dcc8
Showing
1 changed file
with
25 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--- | ||
layout: post | ||
|
||
title: 複製到剪貼版 | ||
tip-number: 56 | ||
tip-username: loverajoel | ||
tip-username-profile: https://twitter.com/loverajoel | ||
tip-tldr: 本週我建立了一個常見的「複製到剪貼版」按鈕,我以前不曾做過這樣的功能,我想分享我如何實作它。 | ||
|
||
categories: | ||
- zh_TW | ||
--- | ||
|
||
這是一個簡單的 tip,本週我建立了一個常見的「複製到剪貼版」按鈕,我以前不曾做過這樣的功能,我想分享我如何實作它。 | ||
它很簡單,比較麻煩的是我們必須加入一個 `<input/>` 與文字複製到 DOM。我們選擇內容並執行 [execCommand](https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand) 複製指令。 | ||
`execCommand('copy')` 將複製時間被選擇的內容 | ||
|
||
這個指令目前[支援](http://caniuse.com/#search=execCommand)所有最新的瀏覽器,允許我們執行其他系統指令,像是:`copy`、`cut`、`paste` 和改變字體顏色、大小等等。 | ||
|
||
```js | ||
document.querySelector('#input').select(); | ||
document.execCommand('copy'); | ||
``` | ||
|
||
具體呈現請看參考[這裡](https://jsbin.com/huhozu/edit?html,js,output) |