gpt4 book ai didi

javascript - 悬停时复制到剪贴板

转载 作者:行者123 更新时间:2023-11-30 11:51:16 24 4
gpt4 key购买 nike

我试图在鼠标悬停时将文本复制到剪贴板,并注意到它仅适用于点击 (document.body.addEventListener('click', copy, true);),但是当您尝试在悬停时触发复制不起作用 (document.body.addEventListener('mouseover', copy, true);)。我在玩这个 example并想知道为什么会这样。

最佳答案

document.execCommand 需要用户事件才能工作。它不会在悬停时起作用,但会在点击等(鼠标按下、鼠标弹起等)时起作用。

您可能仍想检查兼容性 (here)。引用this original answer和 ( this) jsFiddle。似乎浏览器现在确实始终如一地支持它,但您仍然需要确保您想要以表中的那些版本为目标。

$('.big').hover(function () {
// will not work, no user action
$('input').select();
document.execCommand('copy');
});

$('.big').mousedown(function () {
//works
document.execCommand('copy');
});

Copy commands triggered from document.execCommand() will only affect the contents of the real clipboard if the event is dispatched from an event that is trusted and triggered by the user, or if the implementation is configured to allow this. How implementations can be configured to allow write access to the clipboard is outside the scope of this specification.

关于javascript - 悬停时复制到剪贴板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39389776/

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