如何得到 Codemirror 文本区域的值

我正在使用 Codemirror 的文本区插件,但是我不能检索文本区的值。

密码:

var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
lineNumbers: true,
matchBrackets: true,
mode: "text/x-csrc"
});




function showCode()
{
var text = editor.mirror.getCode();
alert(text);
}

它显示出了错误:

editor.getCode() is not a function.
62998 次浏览

尝试使用 getValue()而不是 getCode()

向 getValue (分隔符)中传入一个可选参数,以指定用于分隔行的字符串(默认值为 \n)。

这对我来说没问题。

editor.getValue()

使用 your _ Editor _ instance. getValue () ;

由于在 CodeMirror 中没有名为 getCode()的函数,所以它可以正常工作。

设置值使用 your_editor_instance.setValue();

版本: 5

根据 文件,你现在需要这样做:

doc.getValue(?separator: string) → string

在这个例子中:

editor.getDoc().getValue("\n")

我知道你正在使用 textarea,但我希望这段代码对其他人也有用! 我有这个问题,但与 article标签,这是我的解决方案,以获得所有的代码与 jquery:

res_array = []
$.each($('article.code-draft span[role="presentation"]'), function(){
res_array.push($(this).text())
});
console.log(res_array.join('\n'))

这适用于 C + + Selenium 实例,在这些实例中,您试图捕获在 CodeMirror 文本区域中返回的文本:

var myText = this.WebDriver.ExecuteJavaScript<string>("return $editor[0].getValue()");

其中 [0]是窗体中代码镜像文本区域的索引。

在版本6中,假设你的 EditorView在变量 editor中,然后你做:

editor.state.doc.toString()