HTML dom元素ckeditor标签清理
html dom element ckeditor tag clean up
我有一个问题;当我将ckeditor附加到一个元素,然后尝试使用dom元素检索HTML,我得到脏标记。是否有一些ckeditor功能,清除标记或我需要创建一些自定义的东西?
示例代码:
$(".editor").each(function(){
$(this).attr('contenteditable','true');
var test = CKEDITOR.inline(this);
test.on( 'blur', function( evt ) {
$.post(window.location.href, { task: "change-content", html: $(".content-container")[0].outerHTML })
.done(function( data ) {
//window.location.reload();
}.bind(this));
});
});
html code:
<div class="content-container">
<div class="editor">First editor</div>
<div class="editor">Second editor</div>
<div class="editor">Third editor</div>
</div>
检索到的html函数
<div class="content-container">
<div class="editor cke_editable cke_editable_inline cke_contents_ltr cke_show_borders" tabindex="0" spellcheck="false" role="textbox" aria-label="Rich Text Editor, editor1" title="Rich Text Editor, editor1" aria-describedby="cke_102" style="position: relative;" contenteditable="true">First editor</div>
<div class="editor cke_editable cke_editable_inline cke_contents_ltr cke_show_borders" tabindex="0" spellcheck="false" role="textbox" aria-label="Rich Text Editor, editor1" title="Rich Text Editor, editor1" aria-describedby="cke_102" style="position: relative;" contenteditable="true">Second editor</div>
<div class="editor cke_editable cke_editable_inline cke_contents_ltr cke_show_borders" tabindex="0" spellcheck="false" role="textbox" aria-label="Rich Text Editor, editor1" title="Rich Text Editor, editor1" aria-describedby="cke_102" style="position: relative;" contenteditable="true">Third editor</div>
</div>
是否有一些ckeditor功能,清除这些标签从dom元素?或者我应该创建一个自定义函数?
更新:
愚蠢的解决方案:
var text = $(".content-container")[0].outerHTML;
text = text.replace(/ cke_editable/g, "");
text = text.replace(/ cke_editable_inline/g, "");
text = text.replace(/ cke_contents_ltr/g, "");
text = text.replace(/ cke_show_borders/g, "");
text = text.replace(/ contenteditable='"true'"/g, "");
text = text.replace(/ tabindex='"0'"/g, "");
text = text.replace(/ spellcheck='"false'"/g, "");
text = text.replace(/ role='"textbox'"/g, "");
text = text.replace(/ aria-label='"Rich Text Editor, editor'd+'"/g, "");
text = text.replace(/ title='"Rich Text Editor, editor'd+'"/g, "");
text = text.replace(/ aria-describedby='"cke_'d+'"/g, "");
text = text.replace(/ style='"position: relative;'"/g, "");
text = text.replace(/editor_inline'"/g, "editor");
如果有人有更好的解决方案,请在这里添加:)
你应该使用evt.editor.getData()
-它会给你清理的html。
$(".editor").each(function(){
$(this).attr('contenteditable','true');
var test = CKEDITOR.inline(this);
test.on( 'blur', function( evt ) {
$.post(window.location.href, {
task: "change-content",
html: evt.editor.getData()
})
.done(function( data ) {
//window.location.reload();
}.bind(this));
});
});
更新如果你需要合并所有编辑器的内容,你可以使用:
finalHtml = ''
for (var instanceName in CKEDITOR.instances) {
finalHtml += CKEDITOR.instances[instanceName].getData();
}
您可以将此代码放入test.on( 'blur', function( evt ) {
事件中,并将完整的内容发送到您的服务器(使用$.post
函数):
$(".editor").each(function(){
$(this).attr('contenteditable','true');
var test = CKEDITOR.inline(this);
test.on( 'blur', function( evt ) {
finalHtml = ''
for (var instanceName in CKEDITOR.instances) {
finalHtml += CKEDITOR.instances[instanceName].getData();
}
$.post(window.location.href, {
task: "change-content",
html: finalHtml
})
.done(function( data ) {
//window.location.reload();
}.bind(this));
});
});
相关文章:
- Ckeditor-plugin:插入虚假元素add不情愿<p>标签前后
- Ckeditor:如何在mysql中避免使用Ckeditor中的html标签来节省时间
- 在 Ckeditor 中,删除文本前后的 br 标签
- CKEditor删除和标签
- CKEDITOR 将 替换为 和其他标签
- 在CKEditor 4.x中,有没有办法在初始化后获取允许的标签列表
- ckeditor不允许某些标签
- 当我使用 ckeditor 工具栏时,它会添加 html 标签,而不是在显示时添加所需的效果
- 如何让 CKEditor 允许数据标签标记
- 我可以将 CKEditor 的编辑块包装在几个标签中以模仿上下文吗?
- CkEditor编辑源代码<罢工>标记为<s>标签
- 如何禁用CKEditor以自动添加标签'a'
- 在CKEditor上显示代码,删除abbr标签
- 在 CKEditor 中,如何为按钮添加“文本”标签
- 将光标定位在ckeditor中的标签内
- CKeditor,将标签名称传递给插件中的addcommand
- CKeditor带有url前缀的图像标签
- Ckeditor 4.5.7剥离HTML标签,尽管配置.allowedContent = true
- 在CkEditor中禁用指定标签之间的编辑
- HTML dom元素ckeditor标签清理