前端jQuery復(fù)制文本到剪貼板功能實現(xiàn)
更新時間:2025年01月23日 09:48:47 作者:下頁、再停留
這篇文章主要介紹了前端如何使用jQuery實現(xiàn)點擊“復(fù)制”按鈕時,獲取并復(fù)制父級元素下子元素的文本HTML代碼的功能,文中給出了實現(xiàn)的詳細代碼,需要的朋友可以參考下
功能說明:
點擊“復(fù)制”按鈕,獲取當(dāng)前點擊事件的父級元素(id="block")下子元素(id="textToCopy")下的文本
HTML代碼
<div class="chats">
<div class="block">
<div class="layui-col-xs12 layui-col-sm12 layui-col-md12" style="width: 90%; margin: 0.2rem auto; box-sizing:border-box; display:flex;flex-direction:row;">
<!-- 省略部分代碼 -->
<div id="textToCopy" style="flex: 0.9; line-height: 0.45rem; margin: 0 0.1rem; background: #F5F5F5; padding: 0.1rem; border-radius:5px">
<p>Hello! It seems like you've entered a string of "nnnn." If you have any questions or need assistance</p>
</div>
</div>
<div class="layui-col-xs12 layui-col-sm12 layui-col-md12">
<a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" class="copy-link">復(fù)制</a>
</div>
</div>
<div class="block">
<div class="layui-col-xs12 layui-col-sm12 layui-col-md12" style="width: 90%; margin: 0.2rem auto; box-sizing:border-box; display:flex;flex-direction:row;">
<!-- 省略部分代碼 -->
<div id="textToCopy" style="flex: 0.9; line-height: 0.45rem; margin: 0 0.1rem; background: #F5F5F5; padding: 0.1rem; border-radius:5px">
<p>Hello! please feel free to ask, and I'll be happy to help.</p>
</div>
</div>
<div class="layui-col-xs12 layui-col-sm12 layui-col-md12">
<a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" class="copy-link">復(fù)制</a>
</div>
</div>
</div> Js代碼
document.addEventListener('DOMContentLoaded', function() {
// 為所有 .block 的父元素添加點擊事件監(jiān)聽,使用事件委托處理 .copy-link 的點擊
document.querySelector('.chats').addEventListener('click', function(e) {
// 檢查點擊的元素是否是我們想要的 .copy-link
if (e.target.matches('.copy-link')) {
// 找到點擊的 .copy-link 所在的 .block
var block = e.target.closest('.block');
// 在 .block 中找到 #textToCopy(注意:ID應(yīng)唯一,這里僅為示例)
var textToCopy = block.querySelector('#textToCopy p');
// 獲取文本內(nèi)容
var text = textToCopy.textContent || textToCopy.innerText;
// console.log(text)
// 使用navigator.clipboard API進行復(fù)制(現(xiàn)代瀏覽器)
if (navigator.clipboard) {
navigator.clipboard.writeText(text).then(function() {
console.log('復(fù)制成功');
}, function(err) {
console.error('復(fù)制失敗:', err);
});
} else {
// 對于不支持Clipboard API的瀏覽器,可以使用舊方法(例如創(chuàng)建一個臨時的textarea)
var textarea = document.createElement('textarea');
textarea.value = text;
document.body.appendChild(textarea);
textarea.select();
document.execCommand('copy');
document.body.removeChild(textarea);
console.log('已復(fù)制到剪貼板');
}
}
});
}); 總結(jié)
到此這篇關(guān)于前端jQuery復(fù)制文本到剪貼板功能實現(xiàn)的文章就介紹到這了,更多相關(guān)jq復(fù)制文本到剪貼板內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
jQuery autoComplete插件兩種使用方式及動態(tài)改變參數(shù)值的方法詳解
這篇文章主要介紹了jQuery autoComplete插件兩種使用方式及動態(tài)改變參數(shù)值的方法,結(jié)合實例形式分析了jQuery自動匹配插件autoComplete的使用技巧與動態(tài)改變參數(shù)傳入值的實現(xiàn)方法,需要的朋友可以參考下2016-10-10
基于jQuery Circlr插件實現(xiàn)產(chǎn)品圖片360度旋轉(zhuǎn)
Circlr是一款可以對產(chǎn)品圖片進行360度全方位旋轉(zhuǎn)展示的jQuery插件,本文給大家分享一款基于jQuery Circlr插件實現(xiàn)產(chǎn)品圖片360度旋轉(zhuǎn),大家一起來看看吧2015-09-09
jQuery UI AutoComplete 自動完成使用小記
jQuery UI AutoComplete 自動完成使用小記,使用jquery的朋友實現(xiàn)搜索自動完成等功能的朋友可以參考下。2010-08-08

