我尝试过...
const selBox = document.createElement('textarea');const selBox1 =(selBox);selBox1.style.position ='固定';selBox1.style.left ='0';selBox1.style.top ='0';selBox1.style.opacity ='0';selBox1.value = val;document.body.appendChild(selBox);selBox1.focus();selBox1.select();document.execCommand('copy');document.body.removeChild(selBox)
但是它仅复制内部文本值。我想用相同的CSS属性填充div。用于将其粘贴到邮件或其他地方。
Below is html code::::
<div class="code-bg" id="copyCodeId">
Click on the button to copy the text from the div element. Try to paste the text (e.g. ctrl+v) afterwards in a different window, to see the effect.
</div>
<button (click)="CopyFunction()">Copy div</button>
Below is .ts file function:::
CopyFunction() {
const elm = document.getElementById("copyCodeId");
// for Internet Explorer
if (document.body.createTextRange) {
const range = document.body.createTextRange();
range.moveToElementText(elm);
range.select();
document.execCommand("copy");
alert("Copied div content to clipboard");
} else if (window.getSelection) {
// other browsers
const selection = window.getSelection();
const range = document.createRange();
range.selectNodeContents(elm);
selection.removeAllRanges();
selection.addRange(range);
document.execCommand("copy");
alert("Copied div content to clipboard");
}
}
This is working fine...