我如何使用ReactJS按钮复制文本
您好,我有一个数组,当我单击按钮时,我想复制 p 元素中的文本({item.adress}=> 它是一个数组项)。你能帮我吗?
import classes from './CryptoBox.module.css'
const CryptoBox = (props) => {
let item = props.item
return(
<div className={classes.box}>
{item.map(item => (
<div className={classes.boxx} key={Math.random().toString(36).slice(2)}>
<img src={item.img} alt={item.id}/><br/>
<p id={item.adress} hidden>{item.adress}</p>
<span>
<button onClick={}>
{props.link}
</button>
</span>
</div>
))}
</div>
)
}
export default CryptoBox;
Hello I have an array and I want to copy text in p element ({item.adress}=> it is an array item) when i click the button. Can u help me?
import classes from './CryptoBox.module.css'
const CryptoBox = (props) => {
let item = props.item
return(
<div className={classes.box}>
{item.map(item => (
<div className={classes.boxx} key={Math.random().toString(36).slice(2)}>
<img src={item.img} alt={item.id}/><br/>
<p id={item.adress} hidden>{item.adress}</p>
<span>
<button onClick={}>
{props.link}
</button>
</span>
</div>
))}
</div>
)
}
export default CryptoBox;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
受到这个答案的启发,但可能有一些优化方法。如果你想复制到剪贴板:
Inspired by this answer, but there are probably ways to optimise. If you want to copy to clipboard:
您需要 p 标签吗?因为如果你想复制到剪贴板,你只需要在按钮中执行类似的操作
p 标签对于复制到剪贴板来说不是必需的,因为你实际上拥有
item.address
的值你正在映射,这样你就可以传递它,而不是通过 dom 来查找为用户隐藏的 p 标签。Do you need the p tags? because if you want to copy to the clipboard you just have to do something like this in your button
The p tags are not necessary for copying to the clipboard because you actually have the value of the
item.address
when you're mapping so you can pass that instead of looking through the dom to find the p tag that is hidden for the user.由于我希望您希望根据代码隐藏该段落,因此您可以在 onClick 函数中使用“console.log”来输出您单击的项目的 item.address 。就像下面这样:
Since I expect you want the paragraph to be hidden as per your code, you can just use "console.log" within your onClick function to output the item.address of which ever item you clicked on. Like below: