2

我一直在理解如何为我的tinymce编辑器设置事件处理程序。这篇文章可能会很长,所以我很抱歉。

我正在阅读这篇文章,这正是我想要做的:

我可能会以一种让我对扩展它的功能感到困惑的方式来实例化我的 TinyMCE 编辑器实例。

在我的网站上,我主要让我的编辑做最简单、最基本的工作。现在我在一个页面上,我想反映onkeyup要显示的更改<div id="displayHtml"> (类似于 SOF 正在做的事情)

这是我在大多数页面中通常拥有的内容:

<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="tinymce_configs.js"></script>
</head>

<body>
<div id="displayHtml"></div>

<textarea name="notecomments" id="notecomments" tinymce="tinymce_basic" style="width:650px;height:555px;"></textarea>

<script language="javascript" type="text/javascript">
$(document).ready(function(){ 
    $('textarea[tinymce]').each(function(){ 
        var tinymceopts = $(this).attr('tinymce');
        $(this).tinymce(window[tinymceopts]); 
    });

     // my failed attempt to add an event
     $(".mceContentBody").on('keyup',function(){
        var htmltxt = $(this).val();
        $("#displayHtml").html(htmltxt);
     });

}); 
</script>

</body>
</html>

你会tinymce="tinymce_basic"在我的textarea. 我以这种方式全球化调用 tinymce 主题和插件。对我也很好。

在我的 tinymce_configs.js 文件中,这是我定义的:

// JavaScript Document
var tinymce_basic = 
{
    script_url : '/tinymce/tiny_mce.js',// Location of TinyMCE script
    // General options
    theme : "advanced",
    plugins : "pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,advlist",
    force_p_newlines : false,
    force_br_newlines : true,
    forced_root_block : false, // Needed for 3.x 
    // Theme options
    theme_advanced_buttons1 : "bold,italic,underline,strikethrough,hr,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,fontselect,fontsizeselect,|,styleprops,forecolor,backcolor,|,link,unlink,image,|,bullist,numlist,removeformat,code,|,preview",
    theme_advanced_buttons2 : "",
    theme_advanced_buttons3 : "",
    theme_advanced_buttons4 : "",
    theme_advanced_toolbar_location : "top",
    theme_advanced_toolbar_align : "left",
    theme_advanced_statusbar_location : "bottom",
    theme_advanced_resizing : true,
    theme_advanced_resize_horizontal: false
}
/* more var options below */

所以你有它。以上所有内容都适用于我所有带有 textareas 属性的页面:tinymce="tinymce_basic"。现在是时候关注单个页面了,该页面看起来很棒,tinymce 中的所有更改都反映在<div id="displayHtml">.

我在上面的尝试是寻找.mceContentBody,我认为这会起作用,因为 tinymce 编辑器似乎将 html 保存在该类的 iframe 中。(不起作用)

我对其他人如何启动他们的 Tiny MCE 编辑器感到困惑,因为我的看起来不一样。这是 Tiny MCE 的onKeyUp 示例
我从来没有开始任何事情tinyMCE.init({....

我是否需要更改调用每个 tinymce 编辑器的方式?有了我所拥有的,我怎样才能将我的 TinyMCE 的内容反映到另一个元素中?

以下一些帖子的更改:

<script language="javascript" type="text/javascript">
$(document).ready(function(){

    $('textarea[tinymce]').each(function(){ 
        var tinymceopts = $(this).attr('tinymce');
        $(this).tinymce(window[tinymceopts]); 
    });

     // This was added
     $(".mceContentBody").on('keyup',function(){
            tinymce_basic["setup"] = function(ed) { 
                ed.onKeyUp.add(function(ed, e) { 
                     console.debug('Key up event: ' + e.keyCode); });
            }       

     });

}); 
</script>
4

1 回答 1

4

您需要在配置中执行此操作:

tinyMCE.init({
    mode : ...,
    ...,
    setup : function (ed) {
        ed.onKeyPress.add(
            function (ed, evt) {
                alert("Editor-ID: "+ed.id+"\nEvent: "+evt);
                //....
            }
        );
    },
    ...
});
于 2013-03-24T13:51:39.627 回答