jQuery 可以用于点击复制文本吗?

Can jQuery be used to copy text on click?

我试图让 jQuery 在单击元素时复制元素的标题属性,但我认为我遇到了事件冒泡问题。

我可以使用直接 JS 轻松完成此操作,但我正在尝试了解如何使用 jQuery。

这是我的代码:

<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.1.0.js"></script>
  </head>
  <body>
    <p class="copy" title="actual text to be copied">Hello world.</p>

    <script>
    $('document').ready(function(){

      $(".copy").on({
        click: function(e) {
          document.execCommand("copy");
        },
        copy: function(event) {
          if (event.originalEvent.clipboardData) {
            // allegedly copies the text to the clipboard
            event.originalEvent.clipboardData.setData("text/plain", $(this)[0].title);
            // show us what was copied.
            alert(event.originalEvent.clipboardData.getData("text"));
          }
        }
      });
    });
    </script>
  </body>
</html>

event.clipboardData 不存在,但是 event.originalEvent.clipboardData,所以我正在处理它。

但我认为问题在于 event.originalEvent.clipboardData 实际上不是剪贴板。但是 jQuery 似乎没有将 API 的那部分暴露给它自己的 event.

我是否让 jQuery 将其应用于实际事件而不是 originalEvent?如果是,那又如何呢?

这是一个 jsbin:https://jsbin.com/borumexuga/edit?html,js,output

在 if.

中插入 event.preventDefault();

https://jsbin.com/guwowomece/1/edit?html,js,output