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

JavaScript复制内容到剪贴板 #121

Open
AlexZ33 opened this issue Jun 23, 2021 · 0 comments
Open

JavaScript复制内容到剪贴板 #121

AlexZ33 opened this issue Jun 23, 2021 · 0 comments

Comments

@AlexZ33
Copy link
Owner

AlexZ33 commented Jun 23, 2021

HTML
<input id="demoInput" value="hello world">
<button id="btn">点我复制</button>
JS(安卓)
const btn = document.querySelector('#btn'); 
btn.addEventListener('click', () => {         
    const input = document.querySelector('#demoInput');         
    input.select();         
    if (document.execCommand('copy')) { 
    document.execCommand('copy');                 
    console.log('复制成功');         
    } 
})

IOS
const btn = document.querySelector('#btn'); 
btn.addEventListener('click',() => {         
    const input = document.createElement('input');
    input.setAttribute('readonly', 'readonly');    
    input.setAttribute('value', 'hello world');
    document.body.appendChild(input);         
    input.setSelectionRange(0, 9999);         
    if (document.execCommand('copy')) {
        document.execCommand('copy');                 
        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
None yet
Projects
None yet
Development

No branches or pull requests

1 participant