1. 程式人生 > >原生 js 實現點擊按鈕復制文本

原生 js 實現點擊按鈕復制文本

js實現 復制 ecc 標簽 type man com cti 瀏覽器

以前有個需求就是復制粘貼,今天了解一下一根復制的原生js實現的效果

html:

<p id="text">1234</p>
<input type="text" id="input">
<button onclick="copy()">復制p標簽的文本</button>

js:

function copy ()
{
var text = document.getElementById("text").innerText;
var input = document.getElementById("input");
input.value = text; // 修改文本框的內容
input.select(); // 選中文本
document.execCommand("copy"); // 執行瀏覽器復制命令
alert("復制成功");
}









原生 js 實現點擊按鈕復制文本