使用execCommand应用字体大小

时间:2010-05-04 17:26:30

标签: fonts ckeditor

var wgetFrame = window.frames [0]     wframeDoc = wgetFrame.document;

editor.focus();
editor.execCommand('bold');
wframeDoc.execCommand('forecolor',false,'#00ff00');
wframeDoc.execCommand('JustifyCenter', false, null);
wframeDoc.execCommand('fontsize', false, 15);

(我使用上面的代码作为CKEditor中的插件)

粗体,forecolor和JustifyCenter,它们都是核心的,所选文本由span元素包裹

但是在应用fontsize命令时,所选内容会进入font元素, 我知道这是正确的,但需要它在span元素内

我需要知道为什么粗体,forecolor和JustifyCenter被span和fontsize包裹起来!!

以及是否有另一种方式来应用这种风格

(ps:我在ckeditor初始化时运行这些命令,即使editort不包含任何文本,当你写下已定义的样式时也会应用)


CKEDITOR.editorConfig = function(config) {
CKEDITOR.addStylesSet('customStyles',
[
    { name: 'Header 1', element: 'h1' },
    { name: 'Header 2', element: 'h2' },
    { name: 'Header 3', element: 'h3' },
    { name: 'Text', element: 'p' },
    { name: 'Left Align', element: 'img', attributes: { 'class': 'ImageLeft'} },
    { name: 'Right Align', element: 'img', attributes: { 'class': 'ImageRight'} }
]);

};

我可以应用editor.execCommand(“Header 1”); ??

1 个答案:

答案 0 :(得分:1)

来自

onClick : function( value ) {
    editor.focus();
    editor.fire( 'saveSnapshot' );

    var style = styles[ value ];

    if ( this.getValue() == value )
        style.remove( editor.document );
    else
        style.apply( editor.document );

    editor.fire( 'saveSnapshot' );
}

在_source \ plugins \ font \ plugin.js中我找到了一种方法,但它看起来有点长

editor.focus();
editor.fire('saveSnapshot');

var styles = new CKEDITOR.style({
    element     : 'span',
    styles      : { 'font-size' : '#(size)' },
    overrides   : [ { element : 'font', attributes : { 'size' : null } } ]
}, {size: '72px'});

styles.apply( editor.document );
editor.fire('saveSnapshot');

我现在遇到的问题是编辑焦点,当您在编辑器中吃午餐时,您会看到应用的字体大小,但如果您单击编辑器外部的元素并返回,则光标将离开span style =“72px”并创建一个新的P元素

相关问题