CodeMirror提取代码并使HTML无法正常工作

时间:2015-01-25 22:28:29

标签: javascript jquery html codemirror

我一直在乱用CodeMirror来为我的textarea提供HTML的语法高亮,但我也希望能够单击一个按钮在单独的div中呈现HTML代码。

到目前为止,这是我的功能所在:

    <script>
        /*create the CodeMirror editor*/
        var editor = CodeMirror.fromTextArea(document.getElementById("code"), {lineNumbers: true}); 

        /*Grab the text from the editor and return it*/
        function extractCode() { 
            var text = editor.mirror.getValue();
            return text;
        }
    </script>
    <script>
        /*When the showmeImg image is clicked, grab the code with extractCode and send it to the output div*/
        $(document).ready(function(){
            $("#showmeImg").click(function(){
                $("#output").html($(extractCode).val());
            });
        });
    </script>

我对javascript和jquery相当陌生,所以我不确定我是否正确行事。任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

找到它无法正常工作的原因。由于extractCode已经获取了值,因此在jquery函数的末尾不需要“.val()”。省略.val()可以解决问题。