2012-07-06 61 views
5

我在頁面的textarea上使用TinyMCE。我試圖獲得字符和字數,並輸入textarea。我一直在嘗試各種版本,使其正常工作,但沒有成功。這裏是最新的越野車版本: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.如何?國際海事組織,其中也包括HTML標籤。的HTML代碼是上述輸入的文本如下:

<p>Me&nbsp;</p> 

我期望輸出是1個字和3個字符(2個字符+ 1個空間)。但它顯示了我5個字符。這是怎麼回事&我該如何阻止它?如果您輸入下一個單詞,只要您開始輸入,就會顯示正確的字符數。但是當你再次擊中空間時,它會添加更多字符。所以這個問題似乎在空間&輸入按鈕被擊中時發生。這怎麼解決?

回答

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, '')); 
+0

嗨,感謝您的代碼。我試了上面列出的兩個代碼,同樣的問題仍然存在。出於某種奇怪的原因,它不會刪除HTML標籤。它正在考慮他們。它應該在輸入文本時採用,但是它正在考慮HTML源代碼。我甚至申請了兩次,但效果相同。如果您嘗試使用此代碼,即使在應用替換函數後,您也會看到其提示HTML輸出:'var strip =(tinyMCE.activeEditor.getContent())。替換(/ <\/?[^>] +>/gi,''); \t \t \t \t \t \t警報(strip.replace(/ <\/?[^>] +>/GI, ''));'上的任何想法如何解決這一問題? – Devner 2012-07-06 18:55:31

0

互聯網的內存是無窮無盡的,tinymce上的大部分線程在版本4中都不起作用我的解決方案很簡單:chang e wordcount插件如下: } return e +'Chars:'+ a.getContent()。length} 並通過取消對密鑰代碼的檢查來更改onkeyup代碼32 a.on(「keyup」,function(a ) 就像一個魅力 將這些結果插入到tinymce實例中確實需要一個插件,但這是對示例插件的一個小修改

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