在CKEditor中插入HTML不仅适用于图像

insertHTML in CKEditor does not works only for images

本文关键字:适用于 图像 HTML CKEditor 插入      更新时间:2023-09-26

我尝试使用以下代码将上传的图像插入CKEditor,

var editor = CKEDITOR.instances.writearticle;
var value = '<img src="images/imagename.jpg">';
editor.insertHtml( value );

但这行不通。但是当我尝试使用此代码使用相同的逻辑时

var editor = CKEDITOR.instances.writearticle;
var value = '<strong>Hello World</strong>';
editor.insertHtml( value );

你好世界,因为插入了粗体文本。为什么它不适用于<img>标签?

我在这里找到了这个过程,<img>插入在这个站点中有效。我的网站有什么问题?

添加后问题已解决,

config.allowedContent = 'img[src,alt,width,height]'; // But be sure to add all the other tags you use in with your Editor. Tags except this will be disabled.

替代解决方案

config.extraAllowedContent = 'img[src,alt,width,height]'

这会将<img>属性添加到允许的标签列表中,在这里您需要指定需要允许的每个标签。 - 信用:西布尔

在 config.js 文件中。

就我而言,我只是添加了:

config.allowedContent = true;

在 CKEditor 配置中

它解决了这个问题。

您也可以在此处编写 allowedContent 来修改配置。

editor.addCommand( 'XXXDialog', new CKEDITOR.dialogCommand( 'XXXDialog', { allowedContent : 'img[src,alt,width,height]'}) );