Codemirror 不能使用变量来获取或设置值
Codemirror can't use variable to get or set Value
大家好,我正在使用 Codemirror 编辑器,并在 html textarea 标签内添加了标准代码,然后我在 jQuery 中创建了 Codemirror 元素,但是当我想设置元素值时在 setValue 或 getValue 之外不起作用,因为我定义 codemirror 的变量未定义。我发现的唯一解决方法是使用 codemirror 的 change 实例将 codemirror 实例保存到全局变量,但问题出现了如何在创建时触发 change 方法?如果 codemirror 分配的变量不会未定义任何建议,那就更好了?
<textarea name="mediaopt" rows="8" cols="80">/*===== MEDIA OPTIMIZATION =====*/
</textarea>
editor2 = CodeMirror.fromTextArea($('textarea[name="mediaopt"]')[0], {
lineNumbers: true,
lineWrapping: true,
smartIndent: true,
mode: "text/css",
extraKeys: {"Shift-Space": "autocomplete"},
autoCloseBrackets: true,
matchBrackets: true,
closeTags: true,
theme: "monokai",
}).on('change', editor => {
cssmediaopt = editor.getValue();
globaleditor = editor;
});
在这种情况下,editor2 始终是未定义的编辑器,更改函数内的编辑器是 codemirror 实例,它仅在触发更改方法后才起作用,因此 globaleditor 是未定义的,直到我想要更改的更改为止:)
我建议将每个文本区域包装在具有唯一 ID / class 的 div 中。通过这种方式,您可以指定要让哪个代码镜像实例具有更改事件。
在 .CodeMirror
class.
上使用更改,我更喜欢使用 keyup
您可以在编辑器初始化后直接设置变量。
var editor2 = CodeMirror.fromTextArea($('textarea[name="mediaopt"]')[0], {
lineNumbers: true,
lineWrapping: true,
smartIndent: true,
mode: "text/css",
extraKeys: {"Shift-Space": "autocomplete"},
autoCloseBrackets: true,
matchBrackets: true,
closeTags: true,
theme: "monokai",
}
var cssmediaopt = editor.getValue();
var globaleditor = editor;
$(document).on('change', '.Editor_2 .CodeMirror', function() {
cssmediaopt = editor.getValue();
globaleditor = editor;
});
大家好,我正在使用 Codemirror 编辑器,并在 html textarea 标签内添加了标准代码,然后我在 jQuery 中创建了 Codemirror 元素,但是当我想设置元素值时在 setValue 或 getValue 之外不起作用,因为我定义 codemirror 的变量未定义。我发现的唯一解决方法是使用 codemirror 的 change 实例将 codemirror 实例保存到全局变量,但问题出现了如何在创建时触发 change 方法?如果 codemirror 分配的变量不会未定义任何建议,那就更好了?
<textarea name="mediaopt" rows="8" cols="80">/*===== MEDIA OPTIMIZATION =====*/
</textarea>
editor2 = CodeMirror.fromTextArea($('textarea[name="mediaopt"]')[0], {
lineNumbers: true,
lineWrapping: true,
smartIndent: true,
mode: "text/css",
extraKeys: {"Shift-Space": "autocomplete"},
autoCloseBrackets: true,
matchBrackets: true,
closeTags: true,
theme: "monokai",
}).on('change', editor => {
cssmediaopt = editor.getValue();
globaleditor = editor;
});
在这种情况下,editor2 始终是未定义的编辑器,更改函数内的编辑器是 codemirror 实例,它仅在触发更改方法后才起作用,因此 globaleditor 是未定义的,直到我想要更改的更改为止:)
我建议将每个文本区域包装在具有唯一 ID / class 的 div 中。通过这种方式,您可以指定要让哪个代码镜像实例具有更改事件。
在 .CodeMirror
class.
keyup
您可以在编辑器初始化后直接设置变量。
var editor2 = CodeMirror.fromTextArea($('textarea[name="mediaopt"]')[0], {
lineNumbers: true,
lineWrapping: true,
smartIndent: true,
mode: "text/css",
extraKeys: {"Shift-Space": "autocomplete"},
autoCloseBrackets: true,
matchBrackets: true,
closeTags: true,
theme: "monokai",
}
var cssmediaopt = editor.getValue();
var globaleditor = editor;
$(document).on('change', '.Editor_2 .CodeMirror', function() {
cssmediaopt = editor.getValue();
globaleditor = editor;
});