CKEditor:在粘贴上应用removeFormat
我已经成功地设置了一个粘贴事件来捕获粘贴到文本区域的 HTML。
我需要在将 HTML 粘贴到文本区域之前或之时自动将removeFormat 命令应用于该 HTML,以便我可以去除其中的类、各种标签和其他属性。有人可以指出我正确的方向来正确应用removeFormat命令吗?
到目前为止,这是我的代码:
$(function(){
$('textarea').ckeditor(
function( textarea ){
var editor = this;
editor.on('paste', function( e ) {
//alert(e.data.html); // This shows the HTML
editor.execCommand( 'removeFormat', e.data.html ); // Doesn't seem to do anything, HTML is pasted with the attributes intact
});
}
)
});
谢谢!
PS Force 纯文本选项不可行,因为我希望保留一些 HTML 元素(p、table 等)。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
cf http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR .config.html
You can use
cf http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.config.html
您需要先选择内容,然后才能对其应用removeFormat。
您可以尝试抓取范围(即使只是光标位于插入点)并在粘贴之前保存书签。
粘贴后,使用书签再次选择该范围。
这应该选择您在范围的开始和结束之间粘贴的所有内容。
然后您可以使用removeFormat:
以下是范围和选择API页面的链接:
http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.dom.range.html
http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.dom。 Selection.html
我发现使用范围更容易,createBookmark 方法很好,因为它设置标记,即使 DOM 发生变化,您也可以获取正确的起点和终点(如当您粘贴新内容时就会如此)。您可以在粘贴后使用 moveToBookmark() 来选择范围。
http://docs.cksource.com/ckeditor_api/symbols/ CKEDITOR.dom.range.html#createBookmark
由于文档很少,我发现在源代码中搜索调用方法的位置很有帮助。了解它们的使用方式可以让我更好地了解需要将这些方法应用到哪种类型的对象。
祝你好运,
乔
You need to select the content before you can apply removeFormat to it.
You could try grabbing the range ( even if it's just the cursor sitting at the insertion point ) and saving a bookmark before you paste.
After you paste, use the bookmark to select that range again.
That should select everything that you pasted between the start and end of the range.
Then you can use removeFormat:
Here are the links to the range and selection API pages:
http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.dom.range.html
http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.dom.selection.html
I've found it easier to work with ranges, the createBookmark method is good because it sets markers and you can grab the correct start and end points even if the DOM changes ( as it will when you paste in the new content ). You can use moveToBookmark() after the paste to select the range.
http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.dom.range.html#createBookmark
Because the documentation is sparse, I've found it helpful to search the source code for places where the methods are called. Looking at how they're used gives me a better idea of what kind of object I need to apply the methods to.
Be Well,
Joe
从 CKEditor 4.1 开始,无需进行自定义编码来定义将数据粘贴到 CKEditor 时应保留的元素列表,高级内容过滤器应该可以解决问题。
要么使用默认配置启用 ACF - CKEditor 将接受可以用它创建的所有标签,要么使用或多或少严格的允许标签/属性/样式集定义您自己的规则集。请参阅文档
Starting from CKEditor 4.1 there is no need to do custom coding to define the list of elements that should be kept when pasting data into CKEditor, Advanced Content Filter should do the trick.
Either leave ACF enabled with the default configuration - CKEditor will accept all tags that can be created with it, or define your own set of rules with more or less strict set of allowed tags/attributes/styles. See documentation