簡體   English   中英

CodeMirror-單擊按鈕更改編輯器值

[英]CodeMirror - Change editor value on button click

我試圖在代碼鏡像編輯器中更改該值,但是它不起作用。

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>

您可以嘗試獲取對CodeMirror實例的引用

    function myFunction() {
        var editor = $('.CodeMirror')[0].CodeMirror;
        editor.setValue(fi);
        editor.refresh();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM