这个问题似乎对想要同样东西的人有很多问题,但似乎没有一个答案能够正确解决它(在这里,或在 CKEditor 论坛上),但是我如何获得粘贴的内容而不被粘贴样式属性?
我的目标是几乎粘贴为纯文本,但它会删除表格或粗体标签之类的标签(是的,我想要
em
、strong
、b
等)留下来,我只不这样做不想要样式属性。
我已经使用了格式粘贴插件,但它做了一团乱七八糟的事情,这不是我想要的,并且删除格式按钮还会删除我想保留的所有标签。
您尝试过 CTRL+SHIFT+V 快捷键吗?
我们实现此目的的一种方法是添加
paste
事件侦听器并更新数据:
CKEDITOR.on('instanceReady', function (event) {
event.editor.on('paste', function (ev) {
let temp = document.createElement('div');
temp.innerHTML = ev.data.dataValue;
// get all child tags, regardless of depth
// if you only want to remove the style attribute, can use [style] instead of *
let allChildTags = temp.getElementsByTagName('*');
for (let i = 0; i < allChildTags.length; i++) {
let item = allChildTags.item(i);
item.removeAttribute('style');
// any other changes to these elements can be done here
}
// update the pasted value with the cleaned up HTML
ev.data.dataValue = temp.innerHTML;
temp.remove();
});
});