首页 > 解决方案 > 在网页运行时如何更改 CodeMirror 设置?

问题描述

我正在使用 CodeMirror 将我的文本区域转换为语法荧光笔。代码看起来像这样:

<textarea id="editor">
<html>
<body>
<h1>Some Heading</h1>

</body>
</html>
</textarea>
<input type="checkbox" id="ln" checked/>
<label for="ln">Toggle Line Numbers</label>
<script>
var editor = CodeMirror.fromTextArea(document.getElementById('editor'), {
  mode: "javascript",
  theme: "cobalt",
  lineNumbers: true
});
editor.setSize(1000, 500)
</script>

一切都运行良好。但是配置后我无法更改任何设置。我想知道,当网页运行时,如何将行号更改为关闭或打开或任何其他模式?

标签: javascriptjquerycodemirror

解决方案


您可以在初始化后使用以下方法更改 CodeMirror 设置。

editor.setOption("OPTION_NAME", YOUR_VALUE); // Structure
editor.setOption("lineNumbers", false);      // Example

有关 CodeMirror 选项的更多信息,请参阅CodeMirror 文档站点上的#setOption#event_optionChange 。


推荐阅读