html页面实现使用原生js点击按钮复制文本

<!Doctype html><html xmlns=http://www.w3.org/1999/xhtml>
<head>   
<meta http-equiv=Content-Type content="text/html;charset=utf-8">
<meta http-equiv=X-UA-Compatible content="IE=edge,chrome=1">
<meta content=always name=referrer> 
<link rel="shortcut icon" href=/favicon.ico type=image/x-icon> 
<link rel=icon sizes=any mask href=//www.baidu.com/img/baidu_85beaf5496f291521eb75ba38eacbd87.svg>
<title>点击复制 </title>

</head>  
<body >

<input type="button" onclick="oCopy(this)" value="你好.要copy的内容!">
<script language="javascript">
function oCopy(obj){
alert("呵呵呵");
obj.select();
js=obj.createTextRange();
js.execCommand("Copy")
alert("复制成功!");
}
</script>


<script type="text/javascript">
function copyUrl2()
{
var Url2=document.getElementById("biao1");
Url2.select(); // 选择对象
document.execCommand("Copy"); // 执行浏览器复制命令
alert("已复制好,可贴粘。");
}
</script>
<textarea cols="20" rows="10" id="biao1">用户定义的代码区域</textarea>
<input type="button" onClick="copyUrl2()" value="点击复制代码" />
</body>

猜你喜欢

转载自blog.csdn.net/guo_qiangqiang/article/details/87804093