点击按钮 复制input中的内容

复制按钮效果
点击按钮 复制input中的内容,也就是在input旁边加个按钮,把input的内容复制。

1
2
3
4
5
6
7
8
9
10
<input type="text" id="input" readonly value="点击按钮复制此文本!!"></input>
<button onclick="onCopy()">复制内容</button>
 
<script>
  function onCopy() {
    document.getElementById('input').select()
    document.execCommand('Copy')
    document.getElementById('input').blur()
  }
</script>

这个代码我是在网上找的,参考的这个https://blog.csdn.net/ZGH_77733/article/details/126468057

但是在我的项目中有点问题,点击按钮虽然能复制但是会刷新页面…… 于是做了一个小更改如下。

1
2
3
4
5
6
7
8
9
10
<input type="text" id="inputid" readonly value="点击按钮复制此文本!!"></input>
<input type="button" onclick="onCopy()" value="复制按钮">
 
<script>
  function onCopy() {
    document.getElementById('inputid').select()
    document.execCommand('Copy')
    document.getElementById('inputid').blur()
  }
</script>

可以看到其实就是把button换成了input

此文章的评论已关闭.