admin管理员组文章数量:1023009
how can I copy the content of the element into the clipboard when I click on an element.
<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>
Is it possible to copy without creating any element? Is there a solution similar to this?
$(function (){
$("h1").click(function (){
$(this).select();
document.execCommand("copy");
});
});
How can I do this with jquery or regular javascript. Or what are your other suggestions? Thanks
how can I copy the content of the element into the clipboard when I click on an element.
<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>
Is it possible to copy without creating any element? Is there a solution similar to this?
$(function (){
$("h1").click(function (){
$(this).select();
document.execCommand("copy");
});
});
How can I do this with jquery or regular javascript. Or what are your other suggestions? Thanks
Share Improve this question edited Jun 2, 2021 at 11:31 heybelix asked Jun 2, 2021 at 11:23 heybelixheybelix 571 silver badge4 bronze badges 2- stackoverflow./questions/36639681/… – Muhammad Asif Commented Jun 2, 2021 at 11:25
- Does this solve your issue? 30secondsofcode/blog/s/… – ciekals11 Commented Jun 2, 2021 at 11:25
2 Answers
Reset to default 4You can use the navigator clipboard API. It is supported by all modern browsers.
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();
});
how can I copy the content of the element into the clipboard when I click on an element.
<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>
Is it possible to copy without creating any element? Is there a solution similar to this?
$(function (){
$("h1").click(function (){
$(this).select();
document.execCommand("copy");
});
});
How can I do this with jquery or regular javascript. Or what are your other suggestions? Thanks
how can I copy the content of the element into the clipboard when I click on an element.
<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>
Is it possible to copy without creating any element? Is there a solution similar to this?
$(function (){
$("h1").click(function (){
$(this).select();
document.execCommand("copy");
});
});
How can I do this with jquery or regular javascript. Or what are your other suggestions? Thanks
Share Improve this question edited Jun 2, 2021 at 11:31 heybelix asked Jun 2, 2021 at 11:23 heybelixheybelix 571 silver badge4 bronze badges 2- stackoverflow./questions/36639681/… – Muhammad Asif Commented Jun 2, 2021 at 11:25
- Does this solve your issue? 30secondsofcode/blog/s/… – ciekals11 Commented Jun 2, 2021 at 11:25
2 Answers
Reset to default 4You can use the navigator clipboard API. It is supported by all modern browsers.
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();
});
本文标签: javascriptHow can i copy in clipboard with jqueryStack Overflow
版权声明:本文标题:javascript - How can i copy in clipboard with jquery - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745572151a2156799.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论