Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

js原生实现拷贝到剪贴板 #67

Open
artdong opened this issue Jan 4, 2021 · 1 comment
Open

js原生实现拷贝到剪贴板 #67

artdong opened this issue Jan 4, 2021 · 1 comment
Labels
js javascript

Comments

@artdong
Copy link
Collaborator

artdong commented Jan 4, 2021

js原生实现拷贝到剪贴板

@artdong artdong added the js javascript label Jan 4, 2021
@artdong
Copy link
Collaborator Author

artdong commented Jan 4, 2021

function copyText (text) {
      let input = document.createElement('input');
      input.setAttribute('readonly', 'readonly');
      input.setAttribute('value', text);
      document.body.appendChild(input);
      input.select(); // 不能少
      if (!text || !text.length) {
         console.log('复制内容不能为空!');
         return;
      }
      input.setSelectionRange(0, text.length);
      if (document.execCommand('copy')) {
         document.execCommand('copy');
         console.log('复制成功!');
      } else {
         console.log('复制失败!');
      }
      document.body.removeChild(input);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
js javascript
Projects
None yet
Development

No branches or pull requests

1 participant