CodeMirror不管我做什么都不显示任何代码

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

[好吧,有点背景故事,我想为我的弟弟创建一个小python代码运行程序,因为他不想使用任何IDE或任何东西,所以我做了一个简单的小网站(使用python,这是该语言)我知道python可以正常运行,但是我开始将CodeMirror用作代码编辑器,一切运行正常,我在编辑器上方添加了<nav>元素,并添加了运行按钮(我知道该怎么做,这就是编辑器的问题),现在每次我在其上方使用该按钮运行它时,它都拒绝显示我放入的代码,它的.value设置正确,但是却不显示任何内容,它只是显示此:

CodeMirror Editor not showing anything

现在很烦人,如您所见,我将按钮移到左侧,因为它看起来更好,IMO,但是它什么也没显示,因此我尝试删除首先启动所有按钮的按钮,但是它仍然不会显示任何代码,我正在使用CDN来获取脚本和CSS:

<!-- HEAD -->
<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.54.0/codemirror.min.css'>
<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.54.0/theme/shadowfox.min.css'>
<!-- END -->
<!-- BODY -->
<script src='https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.54.0/codemirror.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.54.0/mode/python/python.min.js'></script>
<!-- END -->

脚本元素在任何html内容之下,并且在我的脚本之上,并且css文件位于我的css文件之下(这不会影响编辑器),并且在头部的底部,以下代码是我用来处理JS的代码初始化编辑器:

// At top of file
var PythonEditor = CodeMirror.fromTextArea(document.getElementById("code-editor"), {
    mode: "python",
    theme: "shadowfox",
    lineNumbers: true,
    indentWithTabs: true,
    indentUnit: 4,
    scrollbarStyle: null
});

// This is in a function that runs when it gets the template code to put in it (it's valid python)
PythonEditor.value = data.Code;
setTimeout(function() {
    PythonEditor.refresh();
},1);

/* data.Code is set to this:
# This will print "Hello World!" to the console
print("Hello World!")
*/

现在编辑器工作正常,我可以很好地编写代码,那里没有问题,但是即使我从Web控制台运行以下命令,它也不会把代码放进去,它什么也没做]


// Just setting the value returns the string
PythonEditor.value = "# Hello there";
setTimeout(function() {
    PythonEditor.refresh(); // Just running this returns 'undefined'
},1);

// Running all that at once returns '12'

我真的很困惑,由于我有一段时间没有使用HTML,CSS和JavaScript了,所以我可能会犯一个菜鸟错误,所以请指出如果我做错了什么(即使它不能解决问题) ,我喜欢建设性的批评)

感谢您阅读

javascript html codemirror
1个回答
0
投票

我做了更多研究,这是经典的“搜索问题而不是解决方案”,答案是使用PythonEditor.getDoc().setValue(INSERT_VALUE);而不是.value = VALUE,然后使用.refresh(),我找到了答案here

© www.soinside.com 2019 - 2024. All rights reserved.