官网是这样介绍的:
Here's a simple example of using RequireJS to load CodeMirror:
require([
"cm/lib/codemirror", "cm/mode/htmlmixed/htmlmixed"
], function(CodeMirror) {
CodeMirror.fromTextArea(document.getElementById("code"), {
lineNumbers: true,
mode: "htmlmixed"
});
});
It will automatically load the modes that the mixed HTML mode depends on (XML, JavaScript, and CSS). Do not use RequireJS' paths option to configure the path to CodeMirror, since it will break loading submodules through relative paths. Use the packages configuration option instead, as in:
require.config({
packages: [{
name: "codemirror",
location: "../path/to/codemirror",
main: "lib/codemirror"
}]
});
我是从http://codemirror.net/codemir...下载的。
我的HTML是
<!doctype html>
<html>
<head>
<title>测试加载 CodeMirror DEMO</title>
<meta charset="utf-8">
<link href="http://cdn.bootcss.com/codemirror/5.23.0/codemirror.min.css" rel="stylesheet">
<script data-main="/requirejsdemo.js" src="http://cdn.bootcss.com/require.js/2.3.2/require.min.js"></script>
</head>
<body>
<textarea id="textarea">
var aa = 'bb';
</textarea>
</body>
</html>
然后requirejsdemo.js的内容是:
require.config({
packages: [{
name: "codemirror",
location: "/public/static/libs/codemirror/lib",
main: 'codemirror'
},{
name: "mode_javascript",
location: "/public/static/libs/codemirror/mode/javascript",
main: 'javascript'
}]
});
require(["codemirror", "mode_javascript"], function(CodeMirror) {
CodeMirror.fromTextArea(document.getElementById("textarea"), {
lineNumbers: true,
mode: "javascript"
});
});
结果,lineNumbers
是生效了,真的出现了行号。然而着色却根本没有。
我似乎是姿势不对,求正确姿势!谢谢!
着色没有是因为你没有引入对应的文件,你引入这个试试