0

基本上,我将 TinyMCE 用于我的文本区域以便于编辑,并且有人为我构建了这个 JS,使 textarea 只能达到 255 个字符。

唯一的问题是它会影响页面上包含的所有文本区域而不是一组文本区域,我不知道 JS,所以如果你们中的一个向导可以指导我添加一个我可以设置要影响的文本区域的位置,我会徘徊吗?

<script type="text/javascript" src="/jscripts/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
tinyMCE.init({
        mode : "textareas",
        theme : "advanced",
        plugins : "bbcode",
        theme_advanced_buttons1 : "bold,italic,underline,undo,redo,link,unlink,image,forecolor,styleselect,removeformat,cleanup,code",
        theme_advanced_buttons2 : "",
        theme_advanced_buttons3 : "",
        theme_advanced_toolbar_location : "bottom",
        theme_advanced_toolbar_align : "center",
        theme_advanced_styles : "Code=codeStyle;Quote=quoteStyle",
    theme_advanced_resizing : true,
    theme_advanced_path : false,
        content_css : "css/bbcode.css",
        entity_encoding : "raw",
        add_unload_trigger : false,
        remove_linebreaks : false,
        inline_styles : false,
        forced_root_block : '',
        convert_fonts_to_spans : false,
        theme_advanced_statusbar_location : "bottom",
    setup: function(ed) {
        ed.onKeyUp.add(function(ed, e) {
        // What is the max amount of characters you want
        var maxChars = 255;
        var content = tinyMCE.activeEditor.getContent();
        // Remove any BBCode tags from the count
        var strip = content.replace(/\[\/?(?:b|i|u|url|quote|code|img|color|size)*?.*?\]/img, '');
        // Set the text for what we want to display in the status bar
        var text = strip.split(' ').length + " Words, " + strip.length + " Characters"
        // Show the status bar message
                tinymce.DOM.setHTML(tinymce.DOM.get(tinyMCE.activeEditor.id + '_path_row'), text);
        // Is there more Characters than we want
        if (strip.length > maxChars) 
        {
            // Show an alert (can comment out)
            alert("Sorry too many characters "  + strip);
            // Get all characters up to the limit
            cur = strip.substring(0,maxChars);
            // Replace content with the max amount
            tinyMCE.activeEditor.setContent(oldContent);
        }

        else
        {
            oldContent = content;

        }

            });

        }
});
4

3 回答 3

2

你需要做的就是

//find the id of the currently active editor
var editor_name=tinyMCE.activeEditor.editorId;

然后

//provide regex for list of editors to be matched
var editors_to_be_matched = /first_editor|third_editor/;

寻找

//if active editor matches against the list of editors
var matched = editor_name.match(editors_to_be_matched);

如果

//active editor does not matches return
if(!matched) return;

别的

//do character count stuff
your code here

所以这就是你的情况

 ed.onKeyUp.add(function(ed, e) {
    var editor_name =tinyMCE.activeEditor.editorId;
    var editors_to_be_matched = /first_editor|third_editor/;
    var matched = editor_name.match(editors_to_be_matched);

    if(!matched) return;
    alert("Do character count stuff here");
    // your code here


    // What is the max amount of characters you want
    var maxChars = 255;
    var content = tinyMCE.activeEditor.getContent();
    ........
    ..........

        });

这是一个演示

于 2012-12-28T06:48:09.173 回答
1

要使 tinymce 仅在某些文本区域上工作,您应该将模式更改为“精确”并在元素参数中指定 html 元素 ID,如下所示:

tinyMCE.init({
        mode : "exact",
        elements : "elm1,elm2",
});

您可以在此处找到有关 tinymce 模式的更多信息:http ://www.tinymce.com/wiki.php/Configuration:mode

于 2012-12-27T20:37:26.553 回答
0

此处的显示名称是新代码,然后您可以检查一下吗:),我想我是对的。

<script type="text/javascript" src="/jscripts/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
tinyMCE.init({
        mode : "textareas",
        theme : "advanced",
        plugins : "bbcode",
        theme_advanced_buttons1 : "bold,italic,underline,undo,redo,link,unlink,image,forecolor,styleselect,removeformat,cleanup,code",
        theme_advanced_buttons2 : "",
        theme_advanced_buttons3 : "",
        theme_advanced_toolbar_location : "bottom",
        theme_advanced_toolbar_align : "center",
        theme_advanced_styles : "Code=codeStyle;Quote=quoteStyle",
    theme_advanced_resizing : true,
    theme_advanced_path : false,
        content_css : "css/bbcode.css",
        entity_encoding : "raw",
        add_unload_trigger : false,
        remove_linebreaks : false,
        inline_styles : false,
        forced_root_block : '',
        convert_fonts_to_spans : false,
        theme_advanced_statusbar_location : "bottom",
    setup: function(ed) {
        ed.onKeyUp.add(function(ed, e) {
        // What is the max amount of characters you want
        var maxChars = 255;
        var content = tinyMCE.activeEditor.getContent();
        var editor_name = tinyMCE.activeEditor.editorId;
        var editors_to_be_matched = /tagline/;
        var matched = editor_name.match(editors_to_be_matched);

        if(!matched) return;

        // Remove any BBCode tags from the count
        var strip = content.replace(/\[\/?(?:b|i|u|url|quote|code|img|color|size)*?.*?\]/img, '');
        // Set the text for what we want to display in the status bar
        var text = strip.split(' ').length + " Words, " + strip.length + " Characters"
        // Show the status bar message
                tinymce.DOM.setHTML(tinymce.DOM.get(tinyMCE.activeEditor.id + '_path_row'), text);
        // Is there more Characters than we want
        if (strip.length > maxChars) 
        {
            // Show an alert (can comment out)
            alert("Sorry too many characters "  + strip);
            // Get all characters up to the limit
            cur = strip.substring(0,maxChars);
            // Replace content with the max amount
            tinyMCE.activeEditor.setContent(oldContent);
        }

        else
        {
            oldContent = content;

        }

            });

        }
});
</script>
于 2012-12-28T12:53:33.690 回答