// 定义变量
this.state={
copy:true,
copyNone:false,
}
//方法
copyValue =()=>{
const copyText = documnet.getElementById('copyValue').innerText
const inputValue = document.createElement("input")
inputValue.value = copyText
document.body.appendChild(inputValue)
inputValue.select()
document.execCommand('Copy')
document.body.removeChild(inputValue)
this.setState({
copy:false,
copyNone:true,
})
this.cpoyChange()
}
cpoyChange = ()=>{
setTimeout(()=>{
this.setState({
copy:true,
copyNone:false,
})
},3000)
}
// html
<span id = "copyValue">复制的内容</span>
{copy &&
<Icon type="copy" onClick={()=>this.copyValue ()} />
}
{copyNone &&
<Icon type="check" />
}
//记得在render中定义 copy 和 copyNone
render(){
const {copy copyNone} = this.state
}
react 复制功能
最新推荐文章于 2024-07-25 01:05:21 发布