【问题标题】:Copy to Clipboard in NodeJs with Express Handlebars template engine使用 Express Handlebars 模板引擎在 NodeJs 中复制到剪贴板
【发布时间】:2019-09-14 19:07:44
【问题描述】:

我们如何在 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

【问题讨论】:

    标签: javascript node.js express-handlebars


    【解决方案1】:

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

    这里有一个解决方案:

    <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/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-18
      • 2010-11-07
      • 1970-01-01
      • 1970-01-01
      • 2015-08-27
      • 2015-08-31
      • 1970-01-01
      相关资源
      最近更新 更多