8000 Update README.MD · AKAChing/fed-code-snippets@7993a9c · GitHub
[go: up one dir, main page]

Skip to content

Commit 7993a9c

Browse files
authored
Update README.MD
1 parent 8ebd4e2 commit 7993a9c

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

CopyText/README.MD

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,34 @@
11
# Copy text
22
> Code
33
``` javascript
4-
function copyText( content = '', successInfo = 'copy success') {
5-
// it works by simulating the input element
6-
let oInput = document.createElement('input')
7-
oInput.id = 'copyInput'
8-
oInput.value = content
9-
document.body.appendChild(oInput)
10-
oInput.select()
11-
document.execCommand('Copy')
12-
oInput.style.display = 'none'
13-
alert(successInfo)
14-
// remove the element after copied
15-
oInput.parentNode.removeChild(oInput)
16-
}
4+
function copyText( content = '') {
5+
return new Promise(resolve => {
6+
setTimeout(() => {
7+
let oInput = document.createElement('input')
8+
oInput.id = 'copyInput'
9+
oInput.value = content
10+
document.body.appendChild(oInput)
11+
oInput.select()
12+
document.execCommand('Copy')
13+
oInput.style.display = 'none'
14+
oInput.parentNode.removeChild(oInput)
15+
resolve()
16+
}, 0)
17+
})
18+
}
1719
```
1820

1921
> Example
2022
``` javascript
2123
`<span id="info">test infomation<span>`
2224
let target = document.getElementById('info')
23-
copyText(target)
25+
copyText(target).then(() => {
26+
console.log('copied success')
27+
})
2428
```
2529

2630
> Params
2731
2832
| Params | Description | Type | Default |
2933
| :------: | :-----------: | :----: | :-------: |
3034
| content | the content you want to copy | string | '' |
31-
| successInfo | the message you want to alert after copied | string | 'copy success' |

0 commit comments

Comments
 (0)
0