TinyMCE字符计数不正确

时间:2012-07-06 15:50:16

标签: jquery tinymce charactercount

我在我的页面中的textarea上使用TinyMCE。我试图获得字符和单词计数,并输入textarea。我一直在尝试各种版本,以使其正常工作,但没有成功。这是最新的错误版本:

$().ready(function() {
    $('textarea.tinymce').tinymce({
        // Location of TinyMCE script
        script_url: 'jscripts/tiny_mce/tiny_mce.js',

        // General options
        theme: "advanced",
        plugins: "autolink,lists,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",

        // Theme options
        theme_advanced_buttons1: "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
        theme_advanced_buttons2: "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
        theme_advanced_buttons3: "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
        theme_advanced_buttons4: "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak",
        theme_advanced_toolbar_location: "top",
        theme_advanced_toolbar_align: "left",
        theme_advanced_statusbar_location: "bottom",
        theme_advanced_resizing: true,

        // Example content CSS (should be your site CSS)
        content_css: "css/content.css",

        // Drop lists for link/image/media/template dialogs
        template_external_list_url: "lists/template_list.js",
        external_link_list_url: "lists/link_list.js",
        external_image_list_url: "lists/image_list.js",
        media_external_list_url: "lists/media_list.js",

        // Replace values for the template plugin
        template_replace_values: {
            username: "Some User",
            staffid: "991234"
        },

        //setup:'tmceWordcount'
        setup: function(ed) {

            ed.onKeyUp.add(function(ed, e) {

                //Following does not work correctly
                //var strip = (tinyMCE.activeEditor.getContent()).replace(/(<([^>]+)>)/ig,"");

                //Neither does the code below
                var strip = (tinyMCE.activeEditor.getContent()).replace(/<[^>]+>/g, "");


                var text = strip.split(' ').length + " Words, " + strip.length + " Characters"
                tinymce.DOM.setHTML(tinymce.DOM.get(tinyMCE.activeEditor.id + '_path_row'), text);
            });
        }

    });
});

这是我的例子。我正在输入第一个字:Me 当我输入第一个单词时,它会正确显示字符和单词。但是只要我按空格键输入下一个单词,就会显示字符是8.如何? IMO,它也包括HTML标签。对于上面输入的文本,HTML代码如下:

<p>Me&nbsp;</p>

我希望输出为1个字和3个字符(2个字符+ 1个空格)。但它向我展示了另外5个角色。这是怎么回事?我怎么能阻止它?如果输入下一个单词,则一旦开始输入,它就会显示正确的字符数。但是当你再次击中空间时,它会添加更多角色。因此,无论什么时候,这个问题似乎都会发生。输入按钮被击中。如何解决这个问题?

4 个答案:

答案 0 :(得分:1)

如果你想做这样的事情,那么必须添加一个像这样的jQuery函数:http://jsfiddle.net/uA9Jx/

jQuery.fn.stripTags = function() {
        return this.replaceWith( this.text().replace(/<\/?[^>]+>/gi, '') );
};

假设这是您的HTML:

<textarea id='bar'>This is <b>bold</b> and this is <i>italic</i>.</textarea>

然后你这样做:

$("#bar").stripTags();

这将导致:

This is bold and this is italic.

或者此代码的工作方式类似:http://jsfiddle.net/cwbMX/

$("#bar").html( $("#bar").text().replace(/<\/?[^>]+>/gi, '') );

答案 1 :(得分:0)

您不必实施自己的解决方案,已经有一个插件同时执行字符和字数:http://adamscheller.com/tinymce-count-characters-plugin/

答案 2 :(得分:0)

互联网的内存是无穷无尽的,并且大多数关于tinymce的线程在版本4中都不起作用 我的解决方案很简单:更改wordcount插件如下: } return e +'Chars:'+ a.getContent()。length} 并通过消除对密钥32的检查来更改onkeyup代码 a.on( “KEYUP”,功能的(a){B()})},0)}), 奇迹般有效 将这些结果插入到tinymce实例中确实需要一个插件,但这只是对示例插件的一个小修改

答案 3 :(得分:0)

对于那些想要使用多个tineMCE实例的人,只需提供一些提示。

...    
setup: function(e){
        //I think in this case 'change' is better than 'keyup' because the user can click a button and change the code without typing.
        e.on('change', function(e){
                var len = tinymce.activeEditor.getContent().length; //Here we get the length of the content with the html tags.
                        var inputName       = tinymce.activeEditor.id; //here we get the name of the input or textarea.

                        var obj = $('input[name="'+inputName+'"]').parent();//Here we set a jquery object reference.

                        var maxLen = 400;
                        obj.find('.char-count').html(len); //Here we set a span tag with the length.

                        if(len > maxLen){
                            obj.find('.char-count').css('color','#fa8072'); //If the len is bigger than maxLen.
                        }else{
                            obj.find('.char-count').css('color','#808080'); //If the len is lower than maxLen.
                        }
                    });
                }