如何将CSS类和ID添加到CKEditor中的段落?
如何向CKEditor中的文本段落添加自定义类或ID?我想从DB中加载可能的类,并在加载CKE时将它们写入其中的任何列表。身份证只需当场补发即可。这些类和ID将用于将一段文本标记为脚注或标题之类的事情如何将CSS类和ID添加到CKEditor中的段落?,ckeditor,Ckeditor,如何向CKEditor中的文本段落添加自定义类或ID?我想从DB中加载可能的类,并在加载CKE时将它们写入其中的任何列表。身份证只需当场补发即可。这些类和ID将用于将一段文本标记为脚注或标题之类的事情 为了清楚起见,我不想使用下拉框更改文本的可见样式,我想添加CSS类,这些类可用于在保存元素后设置元素的样式,具体取决于元素的使用位置。给你。此代码将使用后续ID对段落进行编号,还将为每个尚未分配的段落附加自定义类 var idCounter = 0, pClass = 'myCustom
为了清楚起见,我不想使用下拉框更改文本的可见样式,我想添加CSS类,这些类可用于在保存元素后设置元素的样式,具体取决于元素的使用位置。给你。此代码将使用后续ID对段落进行编号,还将为每个尚未分配的段落附加自定义类
var idCounter = 0,
pClass = 'myCustomClass',
pClassRegexp = new RegExp( pClass, 'g' );
CKEDITOR.replace( 'editor1', {
on: {
instanceReady: function() {
this.dataProcessor.htmlFilter.addRules({
elements: {
p: function( element ) {
// If there's no class, assing the custom one:
if ( !element.attributes.class )
element.attributes.class = pClass;
// It there's some other class, append the custom one:
else if ( !element.attributes.class.match( pClassRegexp ) )
element.attributes.class += ' ' + pClass;
// Add the subsequent id:
if ( !element.attributes.id )
element.attributes.id = 'paragraph_' + ++idCounter;
}
}
});
}
}
});
我已经做过类似的事情(我正在使用CKeditor 4.4.7):
class
在JS中是一个保留字,你应该用引号括起来:element.attributes['class']
我的问题措辞有点错误:P我想让编辑器的当前用户决定给某个元素哪个ID和哪个类。无论如何,谢谢你的意见。我的赏金是为了关注这个问题,所以我把它奖励给它得到的唯一关注:)这正是我想要的:)只需稍加修改(我需要一些SHA1 id),它应该可以完美地工作。非常感谢!:)
editor.addCommand('colSm1', {
exec: function (editor) {
var $element = editor.elementPath().block;
if ($element.getAttribute('class') == null) {
$element.addClass('col-sm-1');
}
});