开发者问题收集

text/html 不带任何样式复制到剪贴板

2021-07-08
1843

我使用此代码复制到剪贴板 htmlLink :

    htmlLink = "<a href='#'>link</a>";
    var copyDiv = document.createElement('div');
    copyDiv.contentEditable = true;
    document.body.appendChild(copyDiv);
    copyDiv.innerHTML = htmlLink;
    copyDiv.unselectable = "off";
    copyDiv.focus();
    document.execCommand('SelectAll');
    document.execCommand("Copy", false, null);
    document.body.removeChild(copyDiv);

但在 tinyMCE 文本编辑器中粘贴后,其中有一些样式。

粘贴返回:

<a style="box-sizing: border-box; color: #1bc5bd; text-decoration-line: none; background-color: #f3f6f9; transition: color 0.15s ease 0s, background-color 0.15s ease 0s, border-color 0.15s ease 0s, box-shadow 0.15s ease 0s, -webkit-box-shadow 0.15s ease 0s; font-family: iransans, tahoma; font-size: 13px; outline: 0px !important;" href="#">link</a>

这些样式来自我不想要的公共网站 css 类,

我如何删除这些样式?

1个回答

我将我的代码更改为此,问题解决。

    htmlLink = "<a href='#'>link</a>";
    function listener(e) {
        e.clipboardData.setData("text/html", htmlLink );
        e.clipboardData.setData("text/plain", htmlLink );
        e.preventDefault();
    }
    document.addEventListener("copy", listener);
    document.execCommand("copy");
    document.removeEventListener("copy", listener);
mehdy.shahbazi
2021-07-08