$ ( ' Body ' ) .on ( ' the Click ' , ' .btn-Copy ' , function () { // var Content = $ (the this) .data ( 'Content'); Data acquired value is an initial value changes back the initial value is acquired or not applicable var Content = $ ( the this ) .attr ( ' Data-Content ' ); IF (! Content) { Content = $ ( the this .) .text () Replace (/ \ S / G, '' ); } IF (Content) { $ ( ' body ' ) .append ( '<input type="text" class="copy-input" value="' + content + '"/>'); $('body .copy-input:last')[0].select(); if (document.execCommand('copy')) { Msg.Notify('复制成功'); } $('body .copy-input').remove(); } });
function copy(str) { console.log(str); function listener(e) { e.clipboardData.setData("text/html", str); e.clipboardData.setData("text/plain", str); e.preventDefault(); } document.addEventListener("copy", listener); document.execCommand("copy"); document.removeEventListener("copy", listener); }