CodeMirror的onLoad函数不起作用?

时间:2010-12-03 22:11:01

标签: javascript codemirror

只是好奇是否有人之前设置过CodeMirror并让onLoad函数工作。使用最新版本时,它无法运行。这是我的代码:

cydonia.editor = CodeMirror.fromTextArea('query', {
    height: $('#query-text-space').css('height'),
    parserfile: ["tokenizexquery.js", "parsexquery.js" ],
    stylesheet: ["/common/codemirror/css/xmlcolors.css", "/common/codemirror/css/xqcolors.css"],
    path: "/common/codemirror/js/",
    continuousScanning: false, //500,
    lineNumbers: true,
    onLoad: function (n) {
        alert('loaded');

    }
}); 

感谢!!!

3 个答案:

答案 0 :(得分:1)

查看代码我没有找到任何onload函数:

   setDefaults(CodeMirrorConfig, {
    stylesheet: [],
    path: "",
    parserfile: [],
    basefiles: ["util.js", "stringstream.js", "select.js", "undo.js", "editor.js", "tokenize.js"],
    iframeClass: null,
    passDelay: 200,
    passTime: 50,
    lineNumberDelay: 200,
    lineNumberTime: 50,
    continuousScanning: false,
    saveFunction: null,
    onChange: null,
    undoDepth: 50,
    undoDelay: 800,
    disableSpellcheck: true,
    textWrapping: true,
    readOnly: false,
    width: "",
    height: "300px",
    minHeight: 100,
    autoMatchParens: false,
    parserConfig: null,
    tabMode: "indent", // or "spaces", "default", "shift"
    enterMode: "indent", // or "keep", "flat"
    electricChars: true,
    reindentOnLoad: false,
    activeTokens: null,
    cursorActivity: null,
    lineNumbers: false,
    firstLineNumber: 1,
    indentUnit: 2,
    domain: null,
    noScriptCaching: false
  });

我知道它在手册中,但不在代码中(codemirror.js)。

答案 1 :(得分:1)

显然,CodeMirror“同步加载,因此只要构造函数返回,就可以使用它。” https://groups.google.com/forum/#!topic/codemirror/oXEdDS5ef64

我无法使用fromTextArea方法运行任何内容,但以下方法对我有用:

var myCodeMirror = CodeMirror(function(elt) {
    el.parentNode.replaceChild(elt, el);
    // onLoad script here
}, {
    value: el.value
});

[UPDATE]

我已经设法使用fromTextArea()来使其工作。最初内容没有出现但使用myCodeMirror.refresh();解决了这个问题。

var myCodeMirror = CodeMirror.fromTextArea(el);
// onLoad script here
myCodeMirror.refresh();

答案 2 :(得分:-3)

只需指定功能名称,请参阅下面的代码

cydonia.editor = CodeMirror.fromTextArea('query', {
    height: $('#query-text-space').css('height'),
    parserfile: ["tokenizexquery.js", "parsexquery.js" ],
    stylesheet: ["/common/codemirror/css/xmlcolors.css", "/common/codemirror/css/xqcolors.css"],
    path: "/common/codemirror/js/",
    continuousScanning: false, //500,
    lineNumbers: true,
    onLoad: codeMirrorLoaded()
}); 

function codeMirrorLoaded(){
    alert("loaded");
}