Tôi đang sử dụng TinyMCE trên một vùng văn bản trong trang của mình. Tôi đang cố gắng để có được nhân vật và số lượng từ và textarea được gõ vào. Tôi đã thử các phiên bản khác nhau để làm cho nó hoạt động chính xác, nhưng không thành công. Đây là phiên bản lỗi mới nhất:Số ký tự TinyMCE không chính xác
$().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);
});
}
});
});
Đây là ví dụ của tôi. Tôi đang nhập từ đầu tiên là: Me
Khi tôi nhập từ đầu tiên, từ đó sẽ hiển thị các ký tự và từ chính xác. Nhưng ngay sau khi tôi nhấn không gian để nhập từ tiếp theo, nó cho thấy các ký tự là 8. Làm thế nào? IMO, bao gồm cả các thẻ HTML. Mã HTML như sau cho văn bản được nhập ở trên:
<p>Me </p>
Tôi mong đợi đầu ra là 1 từ và 3 ký tự (2 ký tự + 1 dấu cách). Nhưng nó cho tôi thấy thêm 5 nhân vật nữa. Làm thế nào điều này xảy ra & làm thế nào tôi có thể ngăn chặn nó? Nếu bạn nhập từ tiếp theo, ngay khi bạn bắt đầu nhập, nó sẽ hiển thị số ký tự chính xác. Nhưng khi bạn nhấn không gian một lần nữa, nó thêm nhiều nhân vật hơn. Vì vậy, vấn đề này dường như xảy ra bất cứ khi nào không gian & nhập các nút được nhấn. Làm thế nào để sửa cái này?
Xin chào, cảm ơn mã. Tôi đã thử cả hai mã được liệt kê ở trên và vấn đề tương tự vẫn tồn tại. Đối với một số lý do kỳ lạ, nó không phải là loại bỏ các thẻ HTML. Nó đang xem xét chúng. Nó nên đã lấy văn bản khi nó được nhập vào nhưng thay vào đó, nó đang xem xét nguồn HTML. Tôi thậm chí còn áp dụng nó hai lần, nhưng vẫn có hiệu ứng tương tự. Nếu bạn thử mã này, bạn sẽ thấy rằng nó cảnh báo đầu ra HTML ngay cả sau khi tôi đã áp dụng hàm thay thế: 'var strip = (tinyMCE.activeEditor.getContent()).thay thế (/ <\/?[^>] +>/gi, ''); \t \t \t \t \t \t alert (strip.replace (/ <\/?[^>] +>/gi, '')); 'Bất kỳ ý tưởng về làm thế nào để sửa lỗi này? – Devner