jquery - 通过单击 jQuery DOM 使用 HTML5 剪贴板 API 复制到剪贴板?

标签 jquery html

我不想使用 flash,而是使用新的 HTML5 Clipboard API。

我想通过点击$('.link-copy')来触发复制事件

我该怎么做?我似乎找不到有效的示例。

最佳答案

以下是如何执行此操作的一个非常基本的示例:

HTML:

<p id="p">Some random text</p>

JavaScript:

copyContent = function (el) {
    // Create a temporary element
    var $tmp = $("<input />");
    // Add the temp el to the DOM
    $("body").append($tmp);
    // Add the text to the temp el and select it
    $tmp.val($(el).text()).select();
    // Tell the broswer to copy the selection
    document.execCommand("copy");
    // Remove the temporary element
    $tmp.remove();
}

点击执行

$('.link-copy').on('click', function() {
    copyContent('#p');
});

Example Fiddle

关于jquery - 通过单击 jQuery DOM 使用 HTML5 剪贴板 API 复制到剪贴板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33614369/

相关文章:

javascript - 如何使用 javascript 删除此头部 css 样式?

javascript - 如何按值复制?

php - 过期的链接

jquery - 我可以在 jquery 内容脚本文件(Firefox 插件 sdk)中使用插件代码吗?如果是怎么办?

javascript - 鼠标滚轮使用部分标签滚动到部分。如何?

javascript - 输入 OnClick 无法识别

javascript - 高度自动在左侧菜单中不起作用

javascript - while循环仅设置php输入字段中的第一个值

php - 有没有办法让图片自动链接到自己?

html - onmouseover改变图像边框