为什么在 TinyMCE 编辑器和 Function 中显示的字数有 1 个字的差异 returns 按 id 编辑器实例的文本统计

Why there is a difference of 1 word in word count shown in TinyMCE editor and Function that returns text statistics for an editor instance by id

我试图限制 TinyMCE 编辑器中的字数。为此使用了 TinyMCE https://www.tiny.cloud/docs-3x/howto/words/ 文档中建议的函数。此函数总是 returns 字数比编辑器状态栏中显示的字数大 1。例如:如果我们清空编辑器,编辑器状态栏中的字数显示为 0,但功能 returns 1。同样,编辑器上的字数为 10,功能 returns 11。为什么该功能 returns 不同的值,如何匹配这些值?

您提供的 link 是针对 TinyMCE 3 文档的,该文档已过时。 TinyMCE 5 是当前发布的编辑器,TinyMCE 6 将很快发布。

在 TinyMCE 4+ 中,您应该使用 API 作为此处记录的 wordcount 插件:
https://www.tiny.cloud/docs/plugins/opensource/wordcount/#api

如果您使用 wordcount API,它将与编辑器中显示的内容相匹配。

var wordcount = tinymce.activeEditor.plugins.wordcount;

console.log(wordcount.body.getWordCount());
console.log(wordcount.body.getCharacterCount());
console.log(wordcount.body.getCharacterCountWithoutSpaces());

console.log(wordcount.selection.getWordCount());
console.log(wordcount.selection.getCharacterCount());
console.log(wordcount.selection.getCharacterCountWithoutSpaces());