javascript - 在网页运行时如何更改 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>
一切都运行良好。但是配置后我无法更改任何设置。我想知道,当网页运行时,如何将行号更改为关闭或打开或任何其他模式?
解决方案
您可以在初始化后使用以下方法更改 CodeMirror 设置。
editor.setOption("OPTION_NAME", YOUR_VALUE); // Structure
editor.setOption("lineNumbers", false); // Example
有关 CodeMirror 选项的更多信息,请参阅CodeMirror 文档站点上的#setOption和#event_optionChange 。
推荐阅读
- jquery - Enable jquery panzoom on some event
- mysql - Normalise ID where 2 FK exist in row
- python - 我正在尝试将循环附加到我的 else 语句
- eclipse - 如何设置编辑器页面的标题(Eclipse)
- android - Linking errors when using functions from
in using API level 22 - typescript - How to explicitly define the return type of a generic returned class in TypeScript?
- java - 使用 Jackson 从嵌套的 JSON 字符串中分配变量
- error-code - 从 cvxpy 中的求解器中检索 mosek 错误代码
- javascript - Node js“请求”模块。“get”和返回值的问题
- javascript - 选择元素内的特定元素并显示文本