Home  >  Article  >  Web Front-end  >  js operation copy text

js operation copy text

php中世界最好的语言
php中世界最好的语言Original
2018-06-08 11:12:151485browse

This time I will bring you the js operation to copy text. What are the precautions for js operation to copy text? The following is a practical case, let’s take a look.

I recently encountered a need to click a button to copy the text in the

tag to the clipboard

I have copied the contents of the input box before. I thought it was almost the same, but I found that It didn’t work at all

I tried various methods, and finally used a blind trick to achieve the following effect

##1. Principle analysis

The browser provides the copy command, which can copy the selected content

document.execCommand("copy")
If it is an input box, you can use the

select() method, Select the text in the input box, and then call the copy command to copy the text to the clipboard

But the select() method is only valid for and