gpt4 book ai didi

javascript - 使用 Express Handlebars 模板引擎在 NodeJs 中复制到剪贴板

转载 作者:行者123 更新时间:2023-11-30 19:35:22 25 4
gpt4 key购买 nike

我们如何在 NodeJs 中使用 express handlebar 模板实现按钮点击时的复制到剪贴板功能。

我尝试过使用 Javascript,但它不起作用。

下面是我试过的代码:

myFile.handlebars :

<input type="button" id="linkBtn" class="btn btn-primary" onclick="copyLink()" data-toggle="tooltip" title="Copy to Clipboard" value="copy link" readonly />

<script>
function copyLink() {
let copyText = document.getElementById("linkBtn");
/* Select the text field */
copyText.select();
/* Copy the text inside the text field */
document.execCommand("copy");

/* Alert the copied text */
//alert("Copied the text: " + copyText.value);
}
</script>

Copy to the clipboard using JS

最佳答案

您试图从按钮复制​​文本。您可以选择按钮文本。在任何其他标签中添加您要选择的内容

解决方法:

<input type="button"  class="btn btn-primary" onclick="copyLink()" data-toggle="tooltip" title="Copy to Clipboard" value="copy link" readonly />

<span id="copyText">Copy this Text</span>

<script>
function copyLink() {
let copyText = document.getElementById("copyText")

var selection = window.getSelection();

var range = document.createRange();

range.selectNodeContents(copyText);

selection.removeAllRanges();

selection.addRange(range);

document.execCommand('copy');
}
</script>

这是一个工作演示:https://jsfiddle.net/5mryvpc6/

关于javascript - 使用 Express Handlebars 模板引擎在 NodeJs 中复制到剪贴板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55998979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com