原生js實(shí)現(xiàn)點(diǎn)擊按鈕復(fù)制內(nèi)容到剪切板
更新時間:2020年11月19日 11:05:38 作者:weixin_44953227
這篇文章主要為大家詳細(xì)介紹了原生js實(shí)現(xiàn)點(diǎn)擊按鈕復(fù)制內(nèi)容到剪切板,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了js點(diǎn)擊按鈕復(fù)制內(nèi)容到剪切板的具體代碼,供大家參考,具體內(nèi)容如下
效果圖

上代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
.divBox {
width: 500px;
margin: 100px auto;
display: flex;
}
.popupStyle {
display: none;
width: 160px;
background-color: rgb(85, 85, 85);
color: aqua;
text-align: center;
border-radius: 6px;
padding: 8px 0;
position: fixed;
z-index: 1;
top: 2%;
left: 50%;
margin-left: -80px;
}
</style>
<body>
<div class="divBox">
<div id="div">這是要復(fù)制的div內(nèi)容</div>
<a href="#" onclick="handleDivCopy()">點(diǎn)擊復(fù)制</a>
</div>
<div class="divBox">
<textarea id="textarea">Hello,World</textarea>
<a href="#" onclick="handleCopy()">點(diǎn)擊復(fù)制</a>
</div>
<script>
// 復(fù)制 div 內(nèi)容
function handleDivCopy() {
const div = document.getElementById("div");
const input = document.createElement("input");
document.body.appendChild(input);
input.value = div.innerText;
input.select();
try {
if (document.execCommand("copy", false)) {
handleDomMsg("div 內(nèi)容復(fù)制成功");
} else {
handleDomMsg("div 內(nèi)容復(fù)制失敗");
}
} catch (error) {
console.log(error, "error")
} finally {
input.remove();
}
};
// 復(fù)制輸入框內(nèi)容
function handleCopy() {
const textarea = document.getElementById("textarea");
textarea.select();
try {
if (document.execCommand("copy", false)) {
handleDomMsg("輸入框內(nèi)容復(fù)制成功");
} else {
handleDomMsg("輸入框內(nèi)容復(fù)制失敗");
}
} catch (error) {
console.log(error, "error")
}
};
// DOM 彈窗
function handleDomMsg(message) {
const div = document.createElement("div");
document.body.appendChild(div);
div.innerHTML = message || "this is a Message";
div.className = "popupStyle";
div.style.display = "block";
setTimeout(() => {
div.remove();
}, 1000);
}
</script>
</body>
</html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- js實(shí)現(xiàn)點(diǎn)擊按鈕復(fù)制文本功能
- js實(shí)現(xiàn)點(diǎn)擊復(fù)制當(dāng)前文本到剪貼板功能(兼容所有瀏覽器)
- js實(shí)現(xiàn)點(diǎn)擊后將文字或圖片復(fù)制到剪貼板的方法
- JS簡單實(shí)現(xiàn)點(diǎn)擊復(fù)制鏈接的方法
- 用 javascript 實(shí)現(xiàn)的點(diǎn)擊復(fù)制代碼
- JavaScript實(shí)現(xiàn)點(diǎn)擊按鈕就復(fù)制當(dāng)前網(wǎng)址
- 點(diǎn)擊進(jìn)行復(fù)制的JS代碼實(shí)例
- JavaScript 點(diǎn)擊觸發(fā)復(fù)制功能實(shí)例詳解
- JavaScript實(shí)現(xiàn)點(diǎn)擊復(fù)制功能具體代碼(JS訪問剪貼板相關(guān))
相關(guān)文章
countup.js實(shí)現(xiàn)數(shù)字動態(tài)疊加效果
這篇文章主要為大家詳細(xì)介紹了countup.js實(shí)現(xiàn)數(shù)字動態(tài)疊加效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-10-10
使用json對象轉(zhuǎn)化為key,value的對象數(shù)組
這篇文章主要介紹了使用json對象轉(zhuǎn)化為key,value的對象數(shù)組方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06
JavaScript實(shí)現(xiàn)連連看連線算法
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)連連看連線算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-01-01
js限制輸入框只能輸入數(shù)字(onkeyup觸發(fā))
這篇文章主要介紹了通過js實(shí)現(xiàn)input輸入框只能輸入數(shù)字的實(shí)現(xiàn)方法,主要是通過正則表達(dá)式替換實(shí)現(xiàn),需要的朋友可以參考下2018-09-09

