2017-09-08 58 views
0

我有4个ckeditor实例在一个页面上,我想在最后一个(justifybloc)设置样式。ckeditor设置justifyblock默认情况下在一个实例没有焦点

现在我用这个代码:

CKEDITOR.on('instanceReady', function(evt) { 

    if (evt.editor.name != undefined) { 
     if (evt.editor.name.indexOf('edit-field-body-1') > -1) { 
     evt.editor.execCommand('justifyblock'); 
     } 
    } 
}); 

的execCommand关注我的textarea的,我不想要的。

我不知道其他方法来做到这一点。

谢谢。

回答

0

我解决我的问题与此解决方案

CKEDITOR.on('instanceReady', function(evt) { 
    if (evt.editor.name != undefined) { 
    if (evt.editor.name.indexOf('edit-field-body-1') > -1) { 
     evt.editor.on('focus', function() { 
     this.execCommand('justifyblock'); 
     }); 
    } 
    } 
} 

Code pen exemple

0

您提供的代码正如您所说的那样工作,重点在于编辑器。此外,由于在编辑器的开头处设置了焦点,因此它只会对齐/对齐第一个块元素(例如段落)。如果没有聚焦编辑器,则无法使用此命令。

然而,要实现证明/对齐只有第一个块元素,您可以使用一个小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'); 
     } 
    } 
}); 

当然你可能总是将选择/聚焦在其他地方。

+0

谢谢您的解决方案! ('focus',function(){ evt.editor.execCommand('justifyblock'); });我使用另一个关于事件 的窍门('focus',function(){ evt.editor.execCommand('justifyblock'); }); – hydrog3n

相关问题