我正在做一個簡單的筆記應用程序並使用GhostDown Markdown編輯器。這很不錯,我喜歡,但我試圖以編程方式設置它的值。如何設置GhostDown Markdown編輯器的值
我可以很容易地得出數值。 $(「入門降價內容textarea的」)。VAL()
設置但它是另一回事... :(
什麼我工作的原型可以在http://potusnotes.com
可以看出我正在做一個簡單的筆記應用程序並使用GhostDown Markdown編輯器。這很不錯,我喜歡,但我試圖以編程方式設置它的值。如何設置GhostDown Markdown編輯器的值
我可以很容易地得出數值。 $(「入門降價內容textarea的」)。VAL()
設置但它是另一回事... :(
什麼我工作的原型可以在http://potusnotes.com
可以看出對於編輯部分,幽靈降價編輯器使用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的Widget工廠生產,一個小部件實例保持它被用來在對象的inside .data("plugin-name")元素。
因此,我們可以這樣訪問控件實例並設置編輯值:
var ghostdown = $(".editor").data("b4m-ghostDown");
ghostdown.editor.setValue("# Hello, SO");
或者乾脆
$(".editor").data("b4m-ghostDown").editor.setValue("# Hello, SO");
你在此[幽靈降價編輯器(https://開頭github上。 com/timsayshey/Ghost-Markdown-Editor)版本? – Blag
@Blag不確定,但如果你提供最新版本的答案,我會確保在必要時進行更新 – Serhiy
它比版本更關注fork(因爲我沒有找到GhostDown Markdown編輯器的官方網站) ;我看了一下源代碼,但有點混亂<_>;它似乎有'getMarkdown'和'getHtml',但沒有setter ...;當他們使用覆蓋,如果你沒有找到正確的方法,這是毫無意義的 – Blag