0

我正在使用 textarea 插入一些文本。一旦达到限制,我想对文本区域进行子串化并将内容设置为同一个编辑器。但它在发送任何文本并设置纯文本之前删除格式。我想要相同的格式。

   tinyMCE.init({
        mode: "textareas",
        theme: "advanced",
        editor_selector: "mceEditor",
        paste_auto_cleanup_on_paste: 'true',
        theme_advanced_disable: 'justifyleft,justifyright,justifyfull,justifycenter,indent,image,anchor,sub,sup,unlink,outdent,help,removeformat,link,fontselect,hr,styleselect,formatselect,charmap,separator,code,visualaid,strikethrough,fullscreen',
        theme_advanced_buttons1: 'bold,italic,underline,numlist,bullist,undo,redo,cleanup,spellchecker',
        theme_advanced_buttons2: "",
        theme_advanced_buttons3: "",
        plugins: 'spellchecker,fullscreen,paste',
        spellchecker_languages: '+English=en-us',
        spellchecker_rpc_url: '<%out.print(request.getContextPath());%>/jazzy-spellchecker',
        theme_advanced_statusbar_location: "bottom",
        theme_advanced_path: false,
        statusbar: true,
        setup: function (editor) {
            editor.onKeyUp.add(function (evt) {
                var inputRichTextArea = $(editor.getBody()).text();
                var maxLengthRichTextArea = document.getElementById(editor.id).maxLength;
                var inputRichTextAreaLength = inputRichTextArea.length;
                if (inputRichTextAreaLength > maxLengthRichTextArea) {
                    inputRichTextAreaLength = maxLengthRichTextArea;
                    editor.setContent("");
                    tinyMCE.activeEditor.selection.setContent(inputRichTextArea.substring(0, maxLengthRichTextArea));
                }
                var value = maxLengthRichTextArea - inputRichTextAreaLength;
                if (value >= 0) {
                    $(editor.getContainer()).find("#" + editor.id + "_path_row").html("Remaining characters: " + (value));
                }
                if (inputRichTextAreaLength >= maxLengthRichTextArea) {
                    $(editor.getBody()).keypress(function (e) {
                        inputRichTextAreaLength = $(editor.getBody()).text().length;
                        if (inputRichTextAreaLength >= maxLengthRichTextArea) {
                            e.stopPropagation();
                            return false;
                        }
                        var value = maxLengthRichTextArea - inputRichTextAreaLength;
                        if (value >= 0) {
                            $(tinyMCE.activeEditor.getContainer()).find("#" + editor.id + "_path_row").html("Remaining characters: " + (value));
                        }
                    });
                }
            });
        }

    });
<textarea maxlength = 50 rows="4" cols="50"></textarea

我怎样才能保持相同的格式。

粘贴文本

格式化文本

4

1 回答 1

0

在您的示例中,您使用该函数.text()获取仅选择没有格式的文本的内容。尝试.getContent()改用,因为它会选择整个内容。

StackOverflow上已经有关于它的问题和答案。请按照那里的说明进行操作。

于 2019-05-19T23:28:19.467 回答