CKEditor:在粘贴时应用removeFormat

Mat*_*teo 8 javascript ckeditor

我已经成功设置了一个on paste事件来捕获粘贴到文本区域的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
                    });              
            }
        )
    });
Run Code Online (Sandbox Code Playgroud)

谢谢!

PS Force纯文本选项不可行,因为我希望保留一些HTML元素(p,表和其他).

gab*_*aam 23

您可以使用

config.forcePasteAsPlainText = true;
Run Code Online (Sandbox Code Playgroud)

cf http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.config.html

  • 我不认为这是 OP 实际问的问题?`forcePasteAsPlainText` 不会删除 _all_ HTML?OP 要求执行`remove format`,它将尝试仅剥离 [`removeFormatAttributes`](http://docs.ckeditor.com/#!/api/CKEDITOR.config-cfg-removeFormatAttributes) 中定义的标签和属性) 和 [`removeFormatTags`](http://docs.ckeditor.com/#!/api/CKEDITOR.config-cfg-removeFormatTags) 如果我没记错?如果我错了,请纠正我。 (2认同)

cod*_*gle 2

您需要先选择内容,然后才能对其应用removeFormat。

您可以尝试抓取范围(即使只是光标位于插入点)并在粘贴之前保存书签。

粘贴后,使用书签再次选择该范围。

这应该选择您在范围的开始和结束之间粘贴的所有内容。

然后你可以使用removeFormat:

editor.execCommand( 'removeFormat', editor.selection );
Run Code Online (Sandbox Code Playgroud)

以下是范围和选择 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

由于文档很少,我发现在源代码中搜索调用方法的位置很有帮助。了解它们的使用方式可以让我更好地了解需要将这些方法应用到哪种类型的对象。

祝你一切顺利,乔