本文共 3288 字,大约阅读时间需要 10 分钟。
引入静态资源:
<#--https://codemirror.net/theme/-->
js 代码实例:
/** * 用来实时对用户的输入进行提示 */function showCodeHint(editor) { editor.on("cursorActivity", function () { //获取用户当前的编辑器中的编写的代码 var words = editor.getValue() + ""; //利用正则取出用户输入的所有的英文的字母 words = words.replace(/[a-z]+[\-|\']+[a-z]+/ig, '').match(/([a-z]+)/ig); //将获取到的用户的单词传入CodeMirror,并在javascript-hint中做匹配 CodeMirror.ukeys = words; //调用显示提示 editor.showHint(); });}$(function () { appendNode(); // 初始化首节点 renderFirstCodeArea(); $('#add-node-btn').unbind().bind('click', () => { appendNode(); renderLastCodeArea(); }); function appendNode() { let firstNodeHtml = getNodeHtml(); $('#node-list').append(firstNodeHtml); } function renderFirstCodeArea() { let inputArray = $('[name="input"]'); let inputEditor = CodeMirror.fromTextArea(inputArray[0], CodeMirrorOptions); showCodeHint(inputEditor); let inputBody = inputEditor.doc.getValue(); let outputArray = $('[name="output"]'); let outputEditor = CodeMirror.fromTextArea(outputArray[0], CodeMirrorOptions); showCodeHint(outputEditor); let outputBody = outputEditor.doc.getValue(); } function renderLastCodeArea() { let inputArray = $('[name="input"]'); let inputArrayLength = inputArray.length; let inputEditor = CodeMirror.fromTextArea(inputArray[inputArrayLength - 1], CodeMirrorOptions); showCodeHint(inputEditor); let inputBody = inputEditor.doc.getValue(); let outputArray = $('[name="output"]'); let outputArrayLength = outputArray.length; let editor = CodeMirror.fromTextArea(outputArray[outputArrayLength - 1], CodeMirrorOptions); showCodeHint(editor); let outputBody = editor.doc.getValue(); } function getNodeHtml() { return ``; }});
参考资料:
CodeMirror实现MySql关键字的变色和自动提示:
Javascript codemirror 高级应用:
转载地址:http://kxlia.baihongyu.com/