我有下面的功能,点击按钮时调用。一切正常,但document.execCommand ('copy')根本不起作用。
如果我创建另一个按钮并仅调用if单独函数中的内容,则效果很好。
我已经尝试在第一个函数中调用第二个函数,但它也不起作用。副本仅在函数中单独存在时才有效。
有谁知道发生了什么?
copyNshort = () => {
const bitly = new BitlyClient('...') // Generic Access Token bit.ly
let txt = document.getElementById('link-result')
bitly.shorten(txt.value)
.then((res) => {
this.setState({ shortedLink: res.url })
if (this.state.shortedLink !== undefined) {
document.getElementById('link-result-shorted').select() // get textarea value and select
document.execCommand('copy') // copy selected
console.log('The link has been shortened and copied to clipboard!')
ReactDOM.render(<i className="fas fa-clipboard-check"></i>, document.getElementById('copied'))
}
console.log('Shortened link ', res.url) // Shorted url
})
}
Run Code Online (Sandbox Code Playgroud)