本文介绍如何利用 JavaScript 实现网页文本复制功能。通过获取输入框元素并触发选中事件,配合 document.execCommand('copy') 命令完成复制操作。示例包含完整的 HTML 结构与 jQuery 事件绑定代码,适用于简单的内容复制场景。
JS实现文本复制功能示例
效果展示如下:

具体实现代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS复制功能</title>
<link href="" rel=" rel="external nofollow" stylesheet">
<style>
*{
margin:0;
padding:0;
}
input{
border:1px solid #ccc;
padding: 5px;
width: 200px;
}
button{
height:32px;
}
</style>
</head>
<body>
<input type="text"placeholder="里面输入文字">
<button type="button">点击复制</button>
<script type="text/javascript" src='http://libs.baidu.com/jquery/2.0.0/jquery.js'></script>
<script>
$(document).ready(function(){
$('button').click(function(){
$('input').trigger('select');
document.execCommand('copy');
alert('复制成功')
})
})
</script>
</body>
</html>
以上内容为本文的全部介绍,期望这些内容能协助大家的学习或工作,同时也感谢各位的支持!