如何像鼠标一样复制html div选择并复制,然后将其粘贴到任何位置.在角度8



我试过。。。

const selBox=document.createElement('extarea'(;const selBox1=(selBox(;selBox1.style.position='fixed';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("副本"(;document.body.removeChild(selBox(

但它只复制内部文本值。我想用相同的CSS属性来填充div。用于将其粘贴到邮件或其他位置。

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");
}
}
<div class="code-bg" id="copyCodeId">
Click on the button to copy the text from the div element. Try to paste the text<span style="color:red"> (e.g. ctrl+v)</span> afterwards in a different window, to see the effect.
</div>
<button onclick="CopyFunction()">Copy div</button>

最新更新