js一键复制功能、几乎兼容所有浏览器

html内容

<div hidden id="keydiv">Hello World</div>

<btn class="btn btn-sm btn-default" onclick="copyText()">点此复制</btn>

js代码

function copyText() {
var Url2 = document.getElementById("keydiv").innerText;
var oInput = document.createElement('input');
oInput.value = Url2;
document.body.appendChild(oInput);
oInput.select();
document.execCommand("Copy");
oInput.className = 'oInput';
oInput.style.display = 'none';
alert('复制成功');
}

猜你喜欢

转载自www.cnblogs.com/ylblogss/p/9644532.html
今日推荐