gpt4 book ai didi

javascript - tinymce - 调整此 js 以仅影响 1 个文本区域

转载 作者:行者123 更新时间:2023-11-28 20:42:53 27 4
gpt4 key购买 nike

基本上,我使用 TinyMCE 作为我的文本区域,以便于编辑,有人为我构建了这个 JS,使文本区域只能包含 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;

}

});

}
});

最佳答案

您需要做的就是

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

关于javascript - tinymce - 调整此 js 以仅影响 1 个文本区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14060473/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com