我有最新版本的 tinymce 编辑器。当我想添加 ed.onKeyDown 部分时,我遇到了问题。在萤火虫控制台我有错误信息:
类型错误:ed.onKeyDown 未定义
之前是我的完整 html 文件:
<textarea id="txtContent"></textarea>
<p>Words left: <span id="txtContent-word-counter">200</span></p>
<script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script>
<script type="text/javascript" src="tinymce\tinymce.jquery.js"></script>
<script type="text/javascript">
tinymce.init({
selector: "textarea",
setup: function(ed) {
var text = '';
var span = document.getElementById(ed.id + '-word-counter');
if(span) {
var wordlimit = span.innerHTML;
ed.onKeyDown.add(function(ed, e) {
text = ed.getContent().replace(/(< ([^>]+)<)/g, '').replace(/\s+/g, ' ');
text = text.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
wordcount = wordlimit - (text.split(' ').length);
span.innerHTML = wordcount;
if(wordcount <= 0 && e.keyCode != 8) {
return tinymce.dom.Event.cancel(e);
}
});
}
}
});
</script>
来源: https ://snipt.net/tamewhale/add-word-count-and-limit-to-tinymce/