【问题标题】:tinymce - adjust this js to affect only 1 textareatinymce - 调整这个 js 只影响 1 个文本区域
【发布时间】:2012-12-13 04:27:00
【问题描述】:

基本上,我将 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;

        }

            });

        }
});

【问题讨论】:

    标签: javascript tinymce


    【解决方案1】:

    你需要做的就是

    //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();
        ........
        ..........
    
            });
    

    这是DEMO

    【讨论】:

    • 我用你的代码添加了一个新的答案,你能仔细检查一下吗?
    【解决方案2】:

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

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

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

    【讨论】:

    • 我不想让 tinymce 只在某些 textareas 上工作,我想要只在某些 textareas 上查看字符数的 js。
    【解决方案3】:

    这里的显示名称是新代码然后你能检查一下吗:),我想我没看错。

    <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>
    

    【讨论】:

      猜你喜欢
      • 2011-07-13
      • 1970-01-01
      • 2023-04-09
      • 2013-05-18
      • 1970-01-01
      • 1970-01-01
      • 2015-02-07
      • 2011-07-30
      • 1970-01-01
      相关资源
      最近更新 更多