codemirror how to install

与世无争的帅哥 提交于 2019-12-05 06:42:36

问题


I am not sure what to do after this:

<link rel="stylesheet" href="lib/codemirror.css">
<script src="lib/codemirror.js"></script>
<script>
  var editor = CodeMirror.fromTextArea(myTextarea, {
    mode: "text/html"
  });
</script>

can someone help me?


回答1:


does this points you to the right direction?

        <link rel="stylesheet" href="lib/codemirror.css">
        <script src="lib/codemirror.js"></script>
        <script src="mode/javascript/javascript.js"></script>
        <script src="addon/fold/foldcode.js"></script>
    </head>
    <body>
        <form style="width:500px;">
            <textarea id="code" name="code">
alert("HI");
//says HII
            </textarea>
        </form>

        <script>
            window.onload = function() {
                window.editor = CodeMirror.fromTextArea(code, {
                    mode: "javascript",
                    lineNumbers: true,
                    lineWrapping: true,
                    foldGutter: {
                        rangeFinder: new CodeMirror.fold.combine(CodeMirror.fold.brace, CodeMirror.fold.comment)
                    },
                    gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"]
                });
            };
        </script>
    </body>
</html>



回答2:


First: you have to select the first element that matches the selector.

$("#editor") won't do it, it has to be $("#editor")[0]

Second: The following code is all you need to get it to work:

window.onload = function () {
    var editor = CodeMirror.fromTextArea($("#editor")[0], {
        lineNumbers: true,
        lineWrapping: true,
    });
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>    
    <head>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.29.0/codemirror.js"></script>
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.29.0/codemirror.css">
        <script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.29.0/mode/javascript/javascript.js"></script>
    </head>
    <body>
        <p>Type some javascript below</p>
        <textarea id="editor"></textarea>
    </body>
</html>


来源:https://stackoverflow.com/questions/21085170/codemirror-how-to-install

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!