codeMirror是一款十分強(qiáng)大的代碼編輯插件,提供了十分豐富的API,最近在項(xiàng)目中用到了這款插件,于是在這里給大家分享下使用方法和心得:
專注于為中小企業(yè)提供成都做網(wǎng)站、成都網(wǎng)站制作、成都外貿(mào)網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)洛隆免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了成百上千家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
codeMirror調(diào)用非常方便
首先在頁面中載入插件CSS及JS文件
<link href="/static/codemirror/lib/codemirror.css" rel="stylesheet" > <script src="/static/codemirror/lib/codemirror.js"></script>
同時(shí)加載你所需要使用的腳本JS及風(fēng)格樣式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頁面中編寫好代碼:
<!--選擇腳本編碼代碼--> <div class="controls"> <input class="ck-code" type="radio" name="script_once_type" id="script_once_type1" checked> shell <input class="ck-code" type="radio" name="script_once_type" id="script_once_type2"> bat <input class="ck-code" type="radio" name="script_once_type" id="script_once_type3"> python </div> <!--選擇腳本風(fēng)格代碼--> <div class="controls"> <select id='select'> <option>default</option> <option>3024-night</option> <option selected>erlang-dark</option> </select> </div> <!--textarea--> <textarea id="script_once_code"> #!/bin/sh </textarea> <textarea id="code2" class="hide"> #!/usr/bin/env python # -*- coding: utf8 -*- </textarea>
調(diào)用關(guān)鍵代碼如下:
var editor = CodeMirror.fromTextArea($("#script_once_code")[0], { //script_once_code為你的textarea的ID號(hào) lineNumbers: true,//是否顯示行號(hào) mode:"shell", //默認(rèn)腳本編碼 lineWrapping:true, //是否強(qiáng)制換行 });
JS配置代碼如下:
//選擇界面風(fēng)格JS $('#select').change(function(){ var theme = $('#select').val(); editor.setOption("theme", theme); //editor.setOption()為codeMirror提供的設(shè)置風(fēng)格的方法 }); //選擇腳本類型JS var txt1=$("#script_once_code").val(); var txt2=''; var txt3=$("#code2").val(); $(".ck-code").click(function(){ var txt=editor.getValue(); //editor.getValue()獲取textarea中的值 var lang=$(this).prop("id"); if(lang=="script_once_type1") { editor.setOption("mode","shell");//editor.setOption()設(shè)置腳本類型 editor.setValue(txt1);// editor.setValue()設(shè)置textarea中的值 } else if(lang=="script_once_type2") { editor.setOption("mode","perl"); editor.setValue(txt2); } else { editor.setOption("mode","python"); editor.setValue(txt3); } });
最終界面如下:
如需配置更多參數(shù),可以訪問codeMirror插件官網(wǎng):http://codemirror.net/ 查看其配置文檔。
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持創(chuàng)新互聯(lián)!
分享名稱:codeMirror插件使用講解
標(biāo)題網(wǎng)址:http://aaarwkj.com/article44/igdohe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、軟件開發(fā)、網(wǎng)站導(dǎo)航、虛擬主機(jī)、云服務(wù)器、電子商務(wù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)