【问题标题】:CodeMirror not showing any code no matter what I do无论我做什么,CodeMirror 都不显示任何代码
【发布时间】:2020-06-05 23:24:19
【问题描述】:

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

现在这很烦人,您可以看到我将按钮移到左侧,因为它看起来更好 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】:

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

    【讨论】:

      猜你喜欢
      • 2015-08-25
      • 2016-10-30
      • 2018-11-22
      • 1970-01-01
      • 2018-01-24
      • 2020-08-18
      • 2012-07-07
      • 1970-01-01
      • 2012-03-11
      相关资源
      最近更新 更多