tinymce - 调整这只影响1 textarea

时间:2012-12-27 19:53:59

标签: javascript tinymce

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

唯一的问题是它影响了一个页面上的所有 textareas而不是一个设置,我知道没有JS所以如果你们中的一个向导可以引导我添加一个我可以设置什么textarea影响?

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

        }

            });

        }
});

3 个答案:

答案 0 :(得分: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();
    ........
    ..........

        });

这是DEMO

答案 1 :(得分:1)

要使tinymce仅在某些textareas上工作,你应该将模式更改为“exact”并在元素参数中指定html元素ID,如下所示:

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

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

答案 2 :(得分: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>
相关问题