gpt4 book ai didi

javascript - 编程语言选择droptown Codemirror

转载 作者:行者123 更新时间:2023-11-30 20:26:28 25 4
gpt4 key购买 nike

我想为 codemirror. 提供一个语言选择下拉菜单我看过this但我无法弄清它的正面或反面。任何人都可以为我清理它或提供解决方案吗?提前致谢!

最佳答案

你可以制作一个对象 {'go':'text/x-go','javascript':'text/javascript'} 等等并索引它。

var editor = CodeMirror.fromTextArea(document.getElementById('edit'),{lineNumbers:true,mode:'text/x-go'});
function select(x) {
var optionText = x.options[x.selectedIndex].innerText;
var modes = {'Golang':'text/x-go','Javascript':'text/javascript','Css':'text/css'};

editor.setOption('mode', modes[optionText]);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.38.0/codemirror.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.38.0/codemirror.min.css" rel="stylesheet"/><script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.38.0/mode/javascript/javascript.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.38.0/mode/go/go.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.38.0/mode/css/css.min.js"></script>

<select onchange="select(this)">
<option>Golang</option>
<option>Javascript</option>
<option>Css</option>
</select> <br>
<textarea id="edit">
//Try do package main and change modes
</textarea>

关于javascript - 编程语言选择droptown Codemirror,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50859731/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com