考虑以下 HTML:
<div class="foo">Hello!</div>
<div class="foo">World!</div>
您可以使用这种方法基于div 元素动态创建编辑器实例。一旦实例被模糊,它就会被销毁,但任何后续点击 div 都会创建一个新实例:
function attachEditorToElement( element ) {
element.once( 'click', function() {
CKEDITOR.replace( element, {
height: 100,
plugins: 'wysiwygarea,toolbar,basicstyles',
on: {
instanceReady: function() {
// Focus the instance immediately.
this.focus();
},
blur: function() {
// Save the reference to element.
var el = this.element;
this.destroy();
// A new instance will be created if div is clicked.
// Note that this is a different element than the one passed to attachEditorToElement.
attachEditorToElement( el );
}
}
} );
} );
}
var divs = CKEDITOR.document.find( 'div.foo' );
// Create editor instances when div is clicked.
for ( var i = divs.count(); i--; )
attachEditorToElement( divs.getItem( i ) );
内联方式:
CKEDITOR.disableAutoInline = true;
function attachEditorToElement( element ) {
element.once( 'click', function() {
if ( element.getEditor() )
return;
element.setAttribute( 'contenteditable', true );
CKEDITOR.inline( element, {
plugins: 'floatingspace,toolbar,basicstyles',
on: {
instanceReady: function() {
// Focus the instance immediately.
this.focus();
},
blur: function() {
var el = this.element;
el.removeAttribute( 'contenteditable' );
this.destroy();
attachEditorToElement( el );
}
}
} );
} );
}
var divs = CKEDITOR.document.find( 'div.foo' );
// Create editor instances when div is clicked.
for ( var i = divs.count(); i--; )
attachEditorToElement( divs.getItem( i ) );