gpt4 book ai didi

javascript - jQuery 可以用来在点击时复制文本吗?

转载 作者:行者123 更新时间:2023-12-01 02:06:44 25 4
gpt4 key购买 nike

我试图让 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 的这一部分暴露给它自己的事件

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

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

最佳答案

在 if 内插入 event.preventDefault();

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

关于javascript - jQuery 可以用来在点击时复制文本吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50051110/

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