【发布时间】:2013-07-12 19:36:41
【问题描述】:
我正在尝试使用 CodeMirror 设置代码示例的样式,但它只能部分工作 - 它将所选主题应用于 textarea,但语法未突出显示。
有我的页面:
<textarea id="template-html" name="code" class="CodeMirror">
<!DOCTYPE html>
<foobar>
<blah>Enter your xml here and press the button below to display it as highlighted by the CodeMirror XML mode</blah>
<tag2 foo="2" bar="bar" />
</foobar>
</textarea>
<link rel="stylesheet" type="text/css" href="/site.com/css/codemirror/codemirror.css">
<link rel="stylesheet" type="text/css" href="/site.com/css/codemirror/theme/ambiance.css">
<link rel="stylesheet" type="text/css" href="/site.com/css/codemirror/theme/solarized.css">
<script type="text/javascript" src="/site.com/js/libs/codemirror/codemirror.js"></script>
<script type="text/javascript" src="/site.com/js/libs/codemirror/mode/javascript/javascript.js"></script>
<script type="text/javascript">
var config, editor;
config = {
lineNumbers: true,
mode: "text/html",
theme: "ambiance",
indentWithTabs: false,
readOnly: true
};
editor = CodeMirror.fromTextArea(document.getElementById("template-html"), config);
function selectTheme() {
editor.setOption("theme", "solarized dark");
}
setTimeout(selectTheme, 5000);
</script>
这是结果的图像。它似乎有效,但没有语法突出显示(图像顶部),我也尝试过不使用我的 CSS,但结果是相同的(图像底部):
问题在于 mode: "text/html" 似乎无法正常工作,如果我使用 mode: "javascript" 它会根据 JavaScript 语法规则对标签进行着色。我该如何解决这个问题?
【问题讨论】:
标签: javascript syntax-highlighting codemirror