2015-11-09 98 views

回答

2

这里是如何做到这一点很简单的例子:

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'); 
}); 

Example Fiddle

+1

这很简单,而且有效。我也喜欢这是一个功能。 – Riki137