我一直在尝试使用javascript将HTML div复制到剪贴板。我想复制整个div并将其粘贴为电子邮件格式。
HTML:
<button class="btn-primary copyToClipboard">Copy Text</button><br /><br /><br /><br />
<div id ="emailContent">
<header style="text-align: center; font-size: 24px;background: #ccc;padding: 10px;width: 100%; box-sizing: border-box">Header Text</header>
<p>This is a Demo Text to Copy and Paste the Content. Please click the link below to activate your account</p>
<a href="#" style="border: 1px solid #4d4d4d; padding: 7px 15px; color: #4d4d4d;text-decoration: none">Click Here</a>
</div>
Javascript:
var copyToClip = document.getElementsByClassName("copyToClipboard")[0];
copyToClip.onclick = function (str) {
function listener(e) {
e.clipboardData.setData("text/html", str);
e.clipboardData.setData("text/plain", str);
e.preventDefault();
}
var txtValue = document.getElementById("emailContent");
document.addEventListener("copy", listener);
document.execCommand("copy");
document.removeEventListener("copy", listener);
}