5

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

$().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

4 回答 4

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, '') );
于 2012-07-06T18:07:56.317 回答
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.
                        }
                    });
                }
于 2015-04-03T22:10:18.390 回答
0

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

于 2013-11-08T13:21:53.947 回答
0

internet的内存是无穷无尽的,tinymce上的大多数线程在版本4中都不起作用我的解决方案很简单:更改wordcount插件如下:}return e + ' Chars: '+a.getContent().length} 并更改onkeyup 代码通过消除对 keycode 32 a.on("keyup",function(a){b()})},0)}) 的检查,就像一个魅力将这些结果插入到 tinymce 实例中确实需要一个插件, 但这是对示例插件的小修改

于 2015-01-26T17:32:32.643 回答