我如何像鼠标选择一样复制html div并复制然后粘贴到任何地方。角度8

问题描述 投票:0回答:1

我尝试过...

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。用于将其粘贴到邮件或其他地方。

javascript jquery copy angular2-routing angular8
1个回答
0
投票
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...
© www.soinside.com 2019 - 2024. All rights reserved.