【问题标题】:Assigning some style from the styles-box in CKEditor through JavaScript通过 JavaScript 从 CKEditor 的样式框中分配一些样式
【发布时间】:2011-05-10 18:02:38
【问题描述】:

如何通过 JS 从样式框中模拟用户选择某些样式?我想放一些快捷按钮,一键分配一些流行的样式。

编辑:

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

【问题讨论】:

  • 我会先搜索 CK Editor API。其次,我只是查看了源代码,看起来样式下拉菜单使用的是<iframe>。此外,样式选择各自使用不同的标签:一些使用<h1><h2>,一些使用<li>...
  • 你想把按钮放在普通的CKEditor工具栏中还是作为页面的外部元素?最简单的方法是使用 styleCommand( style ) 创建命令,为要使用的对象设置样式。
  • 我知道 styleCommand 不能分配标签(如“h1”),而在样式框中选择细线 - 可以(它可以设置标签及其所有属性,包括样式属性)。我不想要内联 CSS;我需要用户从样式框中单击某些样式的相同行为。我不在乎它是来自编辑器内部还是外部。

标签: javascript ckeditor


【解决方案1】:

我没有使用过 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 点击绑定是有效的。

应该差不多吧!我不得不做大量的挖掘工作才能弄清楚这一点,但看到它工作确实很令人满意!

【讨论】:

  • 在您的点击处理程序中,您应该使用 return false;而不是 e.preventDefault 来阻止点击事件冒泡。
  • 在这个例子中,这无关紧要。 “每当元素上发生事件时,该事件也会在每个父元素上触发。”简而言之,这就是冒泡的部分。返回 false 等效于调用 preventDefault() 和 stopPropagation()。我可以想到 preventDefault() 比 return false 更受欢迎的时候。示例来源:css-tricks.com/return-false-and-prevent-default
  • “停止事件流”比冒泡更好。查看此内容以获取有关事件流的更多信息:w3.org/TR/DOM-Level-2-Events/events.html#Events-flow
  • 干得好!非常感谢。所有最后一部分都可以简单地替换为&lt;button onclick="..."&gt;BLAH&lt;/button&gt; 等。然而,第一部分是令人讨厌的事情,我无法让它工作。不幸的是,你的答案离结尾太近了,当我看到它时,赏金已经过去了。 :(
  • 没问题,很高兴能帮上忙。也许我会发现 CKEditor 的用处,因为我不得不对它有点熟悉。 :)
【解决方案2】:

这是一个选项

首先,您可以设置想要在 CSS 类中试用的所需样式。然后,您可以在单击该按钮时为测试 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>

【讨论】:

  • 抱歉,刚刚看到这是专门针对 CKEditor 的,不是一般的 JavaScript 问题。留下答案,以防它帮助别人......
猜你喜欢
  • 2018-03-15
  • 1970-01-01
  • 1970-01-01
  • 2016-01-16
  • 2019-08-18
  • 1970-01-01
  • 2014-09-25
  • 1970-01-01
  • 2018-11-11
相关资源
最近更新 更多