codemirror在单行上呈现代码

时间:2013-02-11 21:50:41

标签: javascript jquery internet-explorer codemirror

http://fiddle.jshell.net/tLth8/6/show/

在IE&lt; 9中,代码突出显示但是新行被忽略,代码全部在一行上,如下所示 How the code is rendered in ie<9

如何正确渲染线条,如下面的源代码

How the code should be rendered, (google chrome)

我试过改变css所以行是display:block;但是没有区别

我尝试了什么

.CodeMirror pre {
    display:block;
}
.CodeMirror-lines div{
    display:block;
}

jQuery源码:

$("span.code, div.code").each(function () {

    var $this = $(this),
        isdiv = $this.is("div"),
        $code = (isdiv) ? $this.html().replace(/^(\r\n)|(\n)/, ''):$this.html(),
        $unescaped = (isdiv) ? $('<div/>').html($code).text() : $('<span/>').html($code).text();

    $this.empty();
    if (isdiv) {
        $this.css({
            "display": "block"
        });
        $this.after('<div class="clear"/>');
    }

    var editor = CodeMirror(this, {
        value: $unescaped,
        mode: (isdiv) ? "text/html" : "javascript",
        lineNumbers: (isdiv) ? true : false,
        readOnly: false
    });
    if (isdiv) {
        var linecount = editor.lineCount();
        for (j = 0; j < linecount; j++) {
            editor.indentLine(j);

        }
    }
});

1 个答案:

答案 0 :(得分:1)

我总是使用textarea标签初始化codemirror。它就是在主站点上完成的。

刚测试它,它在ie8,ie9。

中工作
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
             mode: 'htmlmixed', 
             tabMode: "indent"
});