如何在Textarea中保持文本格式相同



我使用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

我如何保持相同的格式。

粘贴文本

格式化文本

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

在Stackoverflow上已经有问题和答案。请按照那里的说明进行操作。

最新更新