Display formatted raw html with jquery

后端 未结 2 1943
别跟我提以往
别跟我提以往 2021-01-16 03:39

I have a place where I am trying to show code in a text area. I have dumbed down the example but basically user can input some fields, click a button, and the code snippet

相关标签:
2条回答
  • 2021-01-16 04:36

    Add a \n if you want a new line like

    var mySnippet = "<div id=\"myOuterDiv\">\n"
        + "<div id=\"myInnerDiv\">\n"
        + "</div>\n"
        + "</div>\n";
    $('#mySnippetArea').text(eventSnippet);
    

    Single character escape sequences:

    \b: backspace (U+0008 BACKSPACE)
    \f: form feed (U+000C FORM FEED)
    \n: line feed (U+000A LINE FEED)
    \r: carriage return (U+000D CARRIAGE RETURN)
    \t: horizontal tab (U+0009 CHARACTER TABULATION)
    \v: vertical tab (U+000B LINE TABULATION)
    \0: null character (U+0000 NULL)
    

    Example: http://jsfiddle.net/jtx7e/

    0 讨论(0)
  • 2021-01-16 04:36

    just use <pre id="mySnippetArea"></pre> and add \n and space according to the needed layout

    edit:

    you can also write

    var mySnippet =
    "<div id=\"myOuterDiv\">\n\
       <div id=\"myInnerDiv\">\n\
       </div>\n\
    </div>";
    
    0 讨论(0)
提交回复
热议问题