GhostDown Markdown编辑器如何设置值
How to set the value of GhostDown Markdown editor
我正在开发一个简单的笔记应用程序并使用 GhostDown Markdown 编辑器。它非常好,我喜欢它,但我一直试图以编程方式设置它的值。
我可以很容易地得到值。 $('.entry-markdown-content textarea').val()
但是设置它是另一回事... :(
看到我正在研究的原型
对于editor部分,Ghost-Markdown-Editor使用CodeMirror editor。
因此,要以编程方式设置值,我们将调用 CodeMirror 的实例并执行
editor.setValue(txt);
但是如何获取那个CM实例呢?它是由创建 Ghost-Markdown-Editor 的小部件创建的。请参阅 jquery.ghostdown.js 文件:
$.widget( "b4m.ghostDown", {
editor: null,
// ...
_create: function() {
// ...
this.editor = CodeMirror.fromTextArea(this.element.find('textarea')[0], {
mode: 'markdown',
tabMode: 'indent',
lineWrapping: true
});
}
}
由于小部件是使用 jQuery 小部件工厂制作的,因此小部件实例会保留其使用对象的 inside .data("plugin-name") 元素。
因此我们可以像这样访问小部件实例并设置编辑器值:
var ghostdown = $(".editor").data("b4m-ghostDown");
ghostdown.editor.setValue("# Hello, SO");
或者干脆
$(".editor").data("b4m-ghostDown").editor.setValue("# Hello, SO");
我正在开发一个简单的笔记应用程序并使用 GhostDown Markdown 编辑器。它非常好,我喜欢它,但我一直试图以编程方式设置它的值。
我可以很容易地得到值。 $('.entry-markdown-content textarea').val()
但是设置它是另一回事... :(
看到我正在研究的原型对于editor部分,Ghost-Markdown-Editor使用CodeMirror editor。 因此,要以编程方式设置值,我们将调用 CodeMirror 的实例并执行
editor.setValue(txt);
但是如何获取那个CM实例呢?它是由创建 Ghost-Markdown-Editor 的小部件创建的。请参阅 jquery.ghostdown.js 文件:
$.widget( "b4m.ghostDown", {
editor: null,
// ...
_create: function() {
// ...
this.editor = CodeMirror.fromTextArea(this.element.find('textarea')[0], {
mode: 'markdown',
tabMode: 'indent',
lineWrapping: true
});
}
}
由于小部件是使用 jQuery 小部件工厂制作的,因此小部件实例会保留其使用对象的 inside .data("plugin-name") 元素。
因此我们可以像这样访问小部件实例并设置编辑器值:
var ghostdown = $(".editor").data("b4m-ghostDown");
ghostdown.editor.setValue("# Hello, SO");
或者干脆
$(".editor").data("b4m-ghostDown").editor.setValue("# Hello, SO");