File tree Expand file tree Collapse file tree 1 file changed +18
-15
lines changed Expand file tree Collapse file tree 1 file changed +18
-15
lines changed Original file line number Diff line number Diff line change 1
1
# Copy text
2
2
> Code
3
3
``` 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
+ }
17
19
```
18
20
19
21
> Example
20
22
``` javascript
21
23
` <span id="info">test infomation<span>`
22
24
let target = document .getElementById (' info' )
23
- copyText (target)
25
+ copyText (target).then (() => {
26
+ console .log (' copied success' )
27
+ })
24
28
```
25
29
26
30
> Params
27
31
28
32
| Params | Description | Type | Default |
29
33
| :------: | :-----------: | :----: | :-------: |
30
34
| content | the content you want to copy | string | '' |
31
- | successInfo | the message you want to alert after copied | string | 'copy success' |
You can’t perform that action at this time.
0 commit comments