JQuery如何更改CodeMirror中文本区域的字符串值

JQuery how to change the string value of a textarea in CodeMirror

本文关键字:区域 字符串 文本 中文 何更改 CodeMirror JQuery      更新时间:2023-09-26

我正在尝试动态更改文本区域的字符串值。在我的代码中,setValue函数应该这样做,但每当我尝试时,文本区域都会被更改,但之后无法编辑。是什么原因导致了问题?

   function createScriptPanel(value, container, mode) {
        var scriptPanel = $('<div id="editor"><textarea id="editor-code">' + value.script + '</textarea></div>').appendTo(container);
        CodeMirror.commands.autocomplete = function (cm) {
            CodeMirror.showHint(cm, CodeMirror.hint.anyword);
        };
        sincapp.codeEditor = CodeMirror.fromTextArea(document.getElementById("editor-code"), {lineNumbers: true, matchBrackets: true, extraKeys: {"Ctrl-Space": "autocomplete"}, mode: mode});
        scriptPanel.getValue = function () {
            return sincapp.codeEditor.getValue();
        };
        scriptPanel.setValue = function (newName,oldName) {
           var newValue= sincapp.codeEditor.getValue().replace(newName,oldName);
           $('#editor:nth-child(1)').html(newValue);
        };
        return scriptPanel;
    }

因为您调用的是html而不是val而不是$('#editor:nh-child(1)').html(newValue);say$('#editor:nh-child(1)').val(newValue);

这不是关于文本区域,而是关于CodeMirror,我的错误。事实上,这对我来说很简单:

 sincapp.codeEditor.setValue(newValue)

试试这样的东西,

var newValue = sincapp.codeEditor.getDoc().setValue(newName);