网友评分:3分
codemirror是一款非常好用的代码编程插件,可以在线进行代码的编辑,支持语言广泛,可以很方便的在页面中嵌入所需要的代码编辑区,非常的好用,欢迎有需要的碰哟到下载使用!
codeMirror是一款十分强大的代码编辑插件,提供了十分丰富的API。CodeMirror是一个运行在浏览器中的代码编辑器,支持100多种语言,高度可定制。
1.CodeMirror为各种编程语言实现关键字、函数、变量等代码高亮显示,丰富的API和可扩展功能以及多个主题样式。
2.支持语言有C、C++、C#、Java、Perl、HTML、CSS、php、javascript、Python、Lua、Go、Groovy、Ruby等。
3.以及diff、LaTeX、SQL、wiki、Markdown等文件格式。
href="/static/codemirror/lib/codemirror.css" rel="stylesheet"
script src="/static/codemirror/lib/codemirror.js"/script
同时加载你所需要使用的脚本JS及风格样式CSS文件,如下举例:
link href="/static/codemirror/theme/3024-night.css" rel="stylesheet" link href="/static/codemirror/theme/erlang-dark.css" rel="stylesheet"
script src="/static/codemirror/mode/shell/shell.js"/script script src="/static/codemirror/mode/perl/perl.js"/script script src="/static/codemirror/mode/python/python.js"/script
注意文件的放置位置
下一步在html页面中编写好代码:
1 !--选择脚本编码代码-- 2 div 3 input type="radio" name="script_once_type" id="script_once_type1" checked shell 4 input type="radio" name="script_once_type" id="script_once_type2" bat 5 input type="radio" name="script_once_type" id="script_once_type3" python 6 /div 7 8 !--选择脚本风格代码-- 9 div10 select id='select'11 optiondefault/option12 option3024-night/option13 option selectederlang-dark/option14 /select15 /div16 17 !--textarea--18 textarea id="script_once_code"19 #!/bin/sh20 /textarea21 textarea id="code2"22 #!/usr/bin/env python23 # -*- coding: utf8 -*-24 /textarea
调用关键代码如下:
1 var editor = CodeMirror.fromTextArea($("#script_once_code")[0], { /script_once_code为你的textarea的ID号2 lineNumbers: true,/是否显示行号3 mode:"shell", /默认脚本编码4 lineWrapping:true, /是否强制换行5 });
JS配置代码如下:
1 /选择界面风格JS 2 $('#select').change(function(){ 3 var theme = $('#select').val(); 4 editor.setOption("theme", theme); /editor.setOption()为codeMirror提供的设置风格的方法 5 }); 6 7 /选择脚本类型JS 8 var txt1=$("#script_once_code").val(); 9 var txt2='';10 var txt3=$("#code2").val();11 $(".ck-code").click(function(){12 var txt=editor.getValue(); /editor.getValue()获取textarea中的值13 var lang=$(this).PRop("id");14 if(lang=="script_once_type1") {15 editor.setOption("mode","shell");/editor.setOption()设置脚本类型16 editor.setValue(txt1);/ editor.setValue()设置textarea中的值17 }18 else if(lang=="script_once_type2") {19 editor.setOption("mode","perl");20 editor.setValue(txt2);21 }22 else {23 editor.setOption("mode","python");24 editor.setValue(txt3);25 26 }27 });
标签: CodeMirror 代码编辑器 API 编程插件 代码编辑区