[英]How can i copy in clipboard with jquery
單擊元素時,如何將元素的內容復制到剪貼板。
<h1 class="copy"> i want to copy this text on clipboard when I click on an element.</h1>
<p> i want to copy this text on clipboard<p>
是否可以在不創建任何元素的情況下進行復制? 有沒有類似的解決方案?
$(function (){
$("h1").click(function (){
$(this).select();
document.execCommand("copy");
});
});
如何使用 jquery 或常規 javascript 做到這一點。 或者您還有什么其他建議? 謝謝
您可以使用導航 器剪貼板 API 。 所有現代瀏覽器都支持它。
document.querySelector(".copy").onclick = (e) => {
navigator.clipboard.writeText(e.currentTarget.innerText);
}
html:
<h1><button class="copy"> i want to copy this text on clipboard when I click on an element.</button></h1>
jquery:
$(".copy").on('click', function (){
var text = $(this).text()
console.log(text)
var $temp = $("<input>");
$("body").append($temp);
$temp.val(text).select();
document.execCommand("copy");
$temp.remove();
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.