我在使用下面的代码时遇到问题。我不断在控制台中收到错误

I'm having trouble with the code include below. I keep receiving errors in the console

$(".hexData").on("click", function() {
  var inputEle = $("<input>");
  $("body").append(inputEle);
  inputEle.val($(element).text()).select();
  document.execCommand("copy");
  inputEle.remove();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="field hexData"></div>
<div class="button">
  <button class="bttnColor">Copy</button>
</div>

我的这个函数似乎不适用于 div 中的 class hexData。 这是我在控制台中收到的内容:

index.html?url=:125 Uncaught ReferenceError: element is not defined
at HTMLDivElement.<anonymous> (index.html?url=:125)
at HTMLDivElement.dispatch (jquery-3.2.1.min.js:3)
at HTMLDivElement.q.handle (jquery-3.2.1.min.js:3)
(anonymous) @ index.html?url=:125
dispatch @ jquery-3.2.1.min.js:3
q.handle @ jquery-3.2.1.min.js:3

我的目标是使用 jQuery on.('click' 事件复制 class 字段 hexData div 中的文本。

您似乎想要的是:

$('button.bttnColor').on("click", function() {
    var inputEle = $("<input />")
        .appendTo("body")
        .val($('.hexData').text())
        .select();
    document.execCommand("copy");
    inputEle.remove();
});

您最终会得到操作系统剪贴板上 .hexData div 文本的副本。

DEMO