Skip to content

Commit

Permalink
Translate tip-56
Browse files Browse the repository at this point in the history
  • Loading branch information
neighborhood999 committed Aug 9, 2016
1 parent d42c694 commit 9b6dcc8
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions _posts/zh_TW/2016-08-02-copy-to-clipboard.md
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)

0 comments on commit 9b6dcc8

Please sign in to comment.