鼠標(biāo)點(diǎn)擊復(fù)制文本或文本的鏈接代碼。
1、js代碼
<script>
function copyLinkText(event) {
// 獲取觸發(fā)點(diǎn)擊事件的元素
const link = event.target;
// 創(chuàng)建一個(gè)新的臨時(shí)元素,并設(shè)置選區(qū)
const temp = document.createElement("span");
temp.innerHTML = link.textContent;
document.body.appendChild(temp);
// 選中文本
const range = document.createRange();
range.selectNodeContents(temp);
const selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
// 復(fù)制到剪貼板
document.execCommand('copy');
// 清除選區(qū)并移除臨時(shí)元素
selection.removeAllRanges();
document.body.removeChild(temp);
// 可以添加一些用戶反饋,比如彈窗提示復(fù)制成功
alert("鏈接文本已復(fù)制到剪貼板!");
}
</script>2、html代碼
<a href="http://www.qingbaosc.com" onclick="copyLinkText(event)">點(diǎn)擊復(fù)制這個(gè)鏈接文本</a> <a href="http://www.qingbaosc.com"> <span class="copy-link-text">復(fù)制鏈接</span> </a>
我是有底線的
掃描二維碼手機(jī)查看該文章
文章引用:http://www.qingbaosc.com/news/webzhishi/1472.html




