CodeMirror-单击按钮时更改编辑器的值

问题描述 投票:0回答:1

我试图在代码镜像编辑器中更改该值,但是它不起作用。

function myFunction() {
    getFi();//get the new code
    editor.setValue(fi);
    editor.refresh();
}

我也试图更改文本区域的值,但是它不起作用。

function myFunction() {
    getFi();//get the new code
    document.getElementById('code').value = fi;
    editor.refresh(); 
}

编辑:部分代码,这里是编辑器的初始化和应加载代码的函数

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src='http://codemirror.net/lib/codemirror.js'></script>
<script src='http://codemirror.net/mode/xml/xml.js'></script>
<script src='http://codemirror.net/mode/javascript/javascript.js'></script>
<script src='http://codemirror.net/mode/css/css.js'></script>
<script src='http://codemirror.net/mode/htmlmixed/htmlmixed.js'></script>
</head>
<body>
<textarea id="code" name="code" ></textarea>
<button type="button" id="execute" onclick="myFunction()">Load Code</button>
<script>
    var editor = CodeMirror.fromTextArea(document.getElementById('code'), {
        mode: 'text/javascript',
        tabMode: 'indent',
        lineNumbers: true,
        lineWrapping: true,
        autoCloseTags: true,
        indentWithTabs: true
    });

    function myFunction() {
        getFi();//get the new code
        editor.setValue(fi);
        editor.refresh();
    }

    function getFi(){
        /*get new code from a file*/
    }
</script>
</body>
</html>
javascript jquery textarea codemirror
1个回答
0
投票

您可以尝试get a reference to the CodeMirror instance

    function myFunction() {
        var editor = $('.CodeMirror')[0].CodeMirror;
        editor.setValue(fi);
        editor.refresh();
    }
© www.soinside.com 2019 - 2024. All rights reserved.