Home  >  Article  >  Web Front-end  >  How to create a native clipboard in navigator.clipboard browser

How to create a native clipboard in navigator.clipboard browser

php中世界最好的语言
php中世界最好的语言Original
2018-03-19 16:25:003313browse

This time I will bring you how to create a native clipboard in the navigator.clipboard browser. What are the precautions for creating a native clipboard in the navigator.clipboard browser? Here are Let’s take a look at practical cases. Browser native clipboard navigator.clipboard

Write navigator.clipboard.writeText

navigator.clipboard.writeText('Linr Text to be copied')
  .then(() => {
    console.log('Text copied to clipboard');
  })
  .catch(err => {
    // This can happen if the user denies clipboard permissions:
    console.error('Could not copy text: ', err);
});

Read navigator.clipboard.readText

navigator.clipboard.readText()
  .then(text => {
    console.log('Pasted content: ', text);
  })
  .catch(err => {
    console.error('Failed to read clipboard contents: ', err);
  });
document.addEventListener('paste', event => {
  event.preventDefault();
  navigator.clipboard.getText().then(text => {
    console.log('Pasted text: ', text);
  });
});

Believe it or not After reading the case in this article, you have mastered the method. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

Request cross-domain solution CORS


##react-native flatlist pull-up loading onEndReached triggers frequently Solve the problem


How to use the Dom attribute

The above is the detailed content of How to create a native clipboard in navigator.clipboard browser. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn