简体   繁体   English

CodeMirror提取代码并呈现HTML不起作用

[英]CodeMirror extracting code and render HTML not working

I've been messing around with CodeMirror to provide my textarea with syntax highlighting for HTML, but I also want to be able to click a button to render the HTML code in a separate div. 我一直在使用CodeMirror来为我的文本区域提供HTML的语法高亮显示,但是我还希望能够单击一个按钮以在单独的div中呈现HTML代码。

This is what I have for my functions so far: 到目前为止,这是我的功能:

    <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>

I'm fairly new to javascript and jquery so I'm not sure if I'm doing this right at all. 我对javascript和jquery还是比较陌生,所以不确定是否完全正确。 Any help is appreciated! 任何帮助表示赞赏!

Found the reason it wasn't working. 找到了它不起作用的原因。 There's no need for ".val()" at the end of the jquery function, since extractCode already gets the value. jQuery函数的末尾不需要“ .val()”,因为extractCode已经获取了值。 Omitting .val() fixes the issue. 省略.val()可解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM