【问题标题】:CodeMirror extracting code and render HTML not workingCodeMirror 提取代码并渲染 HTML 不起作用
【发布时间】:2015-03-24 09:45:01
【问题描述】:

我一直在使用 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 还很陌生,所以我不确定我是否做得对。任何帮助表示赞赏!

【问题讨论】:

    标签: javascript jquery html codemirror


    【解决方案1】:

    找到它不起作用的原因。在 jquery 函数的末尾不需要“.val()”,因为 extractCode 已经获取了值。省略 .val() 可以解决问题。

    【讨论】:

      猜你喜欢
      • 2013-04-14
      • 2016-02-16
      • 1970-01-01
      • 2014-05-15
      • 1970-01-01
      • 1970-01-01
      • 2017-04-16
      • 2021-10-05
      相关资源
      最近更新 更多