通过JavaScript从CKEditor中的样式框分配一些样式

时间:2011-05-10 18:02:39

标签: javascript ckeditor

如何通过JS模拟样式框中某些样式的用户选择?我想放一些快捷按钮,只需点击一下即可分配一些流行的样式。

编辑:

  • 我不在乎它是编辑器内按钮还是外部按钮。
  • 我不想要 css-style 分配;我想要 CKEditor样式赋值(样式框的那些)。

2 个答案:

答案 0 :(得分:3)

我没有使用过CKEditor,但是,我看到了你的问题并且想到了“这很有趣。”嗯,这是我想出来的:

(是的,我发现了可怕的文档,但是,这不是重点......但是我会给他们提供评论代码的道具。)

///
// function to add buttons that trigger styles to be applied.
//
// editor - CKEDITOR - instance of editor you want command attached to.
// buttonName - String - name of the button
// buttonLabel - String - humane readable name of the button
// commandName - String - name of command, the way to call this command from CKEDITOR.execCommand()
// styleDefinition - StyleDefinition - obj defining the style you would like to apply when this command is called.
///

var addButtonCommand = function( editor, buttonName, buttonLabel, commandName, styleDefiniton )
{
    var style = new CKEDITOR.style( styleDefiniton );
    editor.attachStyleStateChange( style, function( state )
        {
            !editor.readOnly && editor.getCommand( commandName ).setState( state );
        });
    editor.addCommand( commandName, new CKEDITOR.styleCommand( style ) );
    editor.ui.addButton( buttonName,
        {
            label : buttonLabel,

            command : commandName
            //adding an icon here should display the button on the toolbar.
            //icon : "path to img",
        });
};

//Get the editor instance you want to use.  Normally the same as the ID of the textarea CKEditor binds to.
var editor1 = CKEDITOR.instances.editor1;

//If you look at ckeditor/_source/plugins/styles/default.js you will see that this selects the first element.  That list is read into the array 'default'.
var blueTitleStyle = CKEDITOR.stylesSet.registered.default[0];

//Or, you can define the style like this: See http://dev.ckeditor.com/wiki/Components/Styles for more info on style definitions.
var blueTitleStyle = { 
    name : 'Blue Title',
    element : 'h3',
    styles : { 'color' : 'Blue' }
};

addButtonCommand(editor1, 'BlueTitle', 'BlueTitle', 'bluetitle', blueTitleStyle);

这是一个帮助您点击活动的Javascript函数:

//function used to execute the command.  Only used for calling the command when not calling from a button. (Like an A with an onClick bound to it.)
    //pulled this function right out of the api.html example in the ckeditor/_samples dir.
    function ExecuteCommand( commandName )
    {
        // Get the editor instance that we want to interact with.
        var oEditor = CKEDITOR.instances.editor1;

        // Check the active editing mode.
        if ( oEditor.mode == 'wysiwyg' )
        {
            // Execute the command.
            // http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.editor.html#execCommand
            oEditor.execCommand( commandName );
        }
        else
        {
            alert( 'You must be in WYSIWYG mode!' );
        }
    }

现在,您可以创建如下链接:

<a href='#' class='setBlueTitle'>Set Blue Title</a>

并使用一些jQuery来加强它:

 <script type="text/javascript">
            $(document).ready(function(){
                $(".setBlueTitle").onClick(function(e){
                    //stops the click from changing the page and whatever other default action would happen.
                    e.preventDefault();

                    ExecuteCommand('bluetitle');
                });
            });
        </script>

我对按钮图标部分不是100%肯定。我没有一个图标来试试。但是,根据一些帖子,它应该工作正常。无论如何,jQuery点击绑定都可以。

那应该是它!我不得不做很多事情来解决这个问题,但看到它的运作肯定令人满意!

答案 1 :(得分:0)

这是一个选项

首先,您可以在CSS类中设置想要尝试的所需样式。然后,您可以在单击该按钮时为test div设置className。这是一个简单的例子:

<强> test.css:

.bold {
   font-weight: bold; 
}

.italic {
   font-style: italic; 
}

<强>的test.html

<html>
<head>
    <link rel="stylesheet" type="text/css" href="test.css" />
</head>
<body>
    <input type="button" onclick="document.getElementById('testStyleDiv').className='bold'" value="bold"/>
    <input type="button" onclick="document.getElementById('testStyleDiv').className='italic'" value="italic"/>

    <div id="testStyleDiv">foo</div>
</body>
</html>