CodeMirror如何将代码渲染在单行上

5

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);

        }
    }
});

我像避瘟疫一样避免使用Internet Explorer,但你尝试过使用pre标签以外的其他东西吗?你可以使用带有font:monospace的span标签获得相同的结果,但IE可能会更好地处理它。 - Dave
CodeMirror网站上的示例在IE上可以工作,这真的让我感到困惑,直到我检查了源代码,现在我正在使用文本框!! - Yusaf Khaliq
1个回答

1
我一直使用textarea标签来初始化codemirror。这是主站点的做法。
刚测试过,它在ie8、ie9中可以工作。
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
             mode: 'htmlmixed', 
             tabMode: "indent"
});

是的 :) https://dev59.com/3G7Xa4cB1Zd3GeqPolfb#15425629?noredirect=1#comment21420156_14821511 - Yusaf Khaliq

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接