您提供的代码正如您所说的那样工作,重点在于编辑器。此外,由于在编辑器的开头处设置了焦点,因此它只会对齐/对齐第一个块元素(例如段落)。如果没有聚焦编辑器,则无法使用此命令。
然而,要实现证明/对齐只有第一个块元素,您可以使用一个小hackish的解决方案,并手动设置的元素风格(在它是正确的justify
插件识别的方式):
CKEDITOR.on('instanceReady', function(evt) {
if (evt.editor.name != undefined) {
if (evt.editor.name.indexOf('editor1') > -1) {
evt.editor.editable().getChild(0).setStyle('text-align', 'justify');
}
}
});
见working codepen。
这样的解决方案有一些缺点(例如,它假定编辑器中的第一个元素是块元素 - 在大多数情况下是这样,等等),但是在大多数情况下应该足够了。
顺便说一句,如果编辑内容被选中的事实不打扰你,你想证明/对齐全部内容,它必须首先选择(selectall
插件需要),如:
CKEDITOR.on('instanceReady', function(evt) {
if (evt.editor.name != undefined) {
if (evt.editor.name.indexOf('editor1') > -1) {
evt.editor.execCommand('selectAll');
evt.editor.execCommand('justifyblock');
}
}
});
当然你可能总是将选择/聚焦在其他地方。
谢谢您的解决方案! ('focus',function(){ evt.editor.execCommand('justifyblock'); });我使用另一个关于事件 的窍门('focus',function(){ evt.editor.execCommand('justifyblock'); }); – hydrog3n