
本文教程操作环境:windows7系统、jquery3.2.1版本,DELL G3电脑。
在编程中,实现代码的在线功能是非常迫切的需求。在js中有这样一个编辑器,能够实现代码的在线高亮,很多人都忽略了它的存在。这个编辑器就是codemirror,如果使用过一些基础库的人,可能见过它的身影。下面我们就codemirror概念、用法,以及配置项的在线运行为大家展开讲解。
1.codemirror概念
codemirror 基于Javascript,短小精悍,实时在线代码高亮显示,它不是某个富文本编辑器的附属产品,它是许多大名鼎鼎的在线代码编辑器的基础库。
2.codemirror用法
1 2 3 4 5 6 7 | var editor = CodeMirror.fromTextArea(document.getElementById( "htmlEdit" ), {
lineNumbers: false ,
mode: "htmlmixed" ,
indentUnit: 2,
lineWrapping: true ,
styleActiveLine: true
});
|
3.codemirror配置项
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | cmOptions: {
flattenSpans: false ,
tabSize: 2,
mode: '' ,
theme: 'monokai' ,
smartIndent: true ,
lineNumbers: true ,
matchBrackets: true ,
lineWiseCopyCut: true ,
indentWithTabs: true ,
electricChars: true ,
indentUnit: 2,
autoCloseTags: true ,
autoCloseBrackets: true ,
foldGutter: true ,
cursorHeight: 1,
keyMap: 'sublime' ,
extraKeys: {
'Ctrl-Alt' : 'autocomplete' ,
'Ctrl-Q' : cm => {
cm.foldCode(cm.getCursor())
}
},
gutters: [ 'CodeMirror-linenumbers' , 'CodeMirror-foldgutter' ],
styleActiveLine: true
},
|
以上就是js代码在线运行,我们可以使用codemirror编辑器的一些操作。有这类需求的人,可以尝试使用codemirror的一些基础用法。