点击jQuery DOM,使用HTML5剪贴板API复制到剪贴板?
问题描述:
我不想使用闪光灯,但使用新的HTML5剪贴板API。点击jQuery DOM,使用HTML5剪贴板API复制到剪贴板?
我想通过点击$('.link-copy')
我如何做到这一点火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();
}
执行上click
$('.link-copy').on('click', function() {
copyContent('#p');
});
+1
这很简单,而且有效。我也喜欢这是一个功能。 – Riki137
而问题是? – Andreas