~~~
<div id="div">
<p>我的推廣鏈接:</p>
<input id="input" value="http://www.baidu.com" readonly/>
<button onclick="copyUrl()">點擊復制鏈接</button>
</div>
function copyUrl() {
var Url=document.getElementById("input");
Url.select(); // 選擇對象
document.execCommand("Copy"); // 執行瀏覽器復制命令
layer.msg("復制成功!");
}
~~~
1、`<input>`標簽里的值應該設置為只讀,所以加了屬性readonly。一開始我加了disabled,發現不能復制,后來查了,用readonly才能復制。
2、代碼的最后一行用了layer插件,插件地址:http://layer.layui.com/