2012-12-27 34 views
0

基本上我使用TinyMCE爲我的文本區域更容易編輯和有人構建這個JS爲我做一個textarea只有255個字符。tinymce - 調整這js隻影響1 textarea

唯一的問題是,它影響其列入,而不是一組一個在頁面上所有文字區域,我不知道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; 

     } 

      }); 

     } 
}); 

回答

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

+0

我用你的代碼添加了一個新的答案,你可以仔細檢查? – NaughtySquid

1

,使上只有一些文字區域TinyMCE的工作,你應該改變模式,以「精確」和指定的HTML元素ID的元素參數是這樣的:

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

你可以找到關於此TinyMCE的模式的詳細信息:http://www.tinymce.com/wiki.php/Configuration:mode

+0

我不希望tinymce只在一些textareas上工作,我想要的只是在一些textareas上看字符數的js。 – NaughtySquid

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

事實上它確實工作得很好:D – NaughtySquid