text/html复制到剪贴板,没有任何样式



我使用此代码复制到剪贴板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类,

如何删除这些样式?

我将代码更改为this并修复了problex。

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);

最新更新