Using
 and  tags to display a javascript script

后端 未结 4 2097
南笙
南笙 2020-12-19 02:29

I\'m experimenting with the < pre> and < code> tags in html 5 as I would like to include some code snippets on my website. I\'m using the page below as a test page but

相关标签:
4条回答
  • 2020-12-19 02:44

    This would work if you take the script tags out.

    These code tags only change the font of the text inside to a monospace font, however it does not override the interpretation of other tags (or even php tags).

    A better way is to use CSS to get color highlighting, or javascript libraries.

    0 讨论(0)
  • 2020-12-19 02:53

    Remove script tag:

    <body>
        <div style="color:#000000">
          <pre>
            <code>
                $('#inputField').hide();
            </code>
          </pre>
        </div>
    </body>
    
    0 讨论(0)
  • 2020-12-19 02:53

    It was my understanding that using these new tags would negate any code that they contain

    They don't. They tell user agents to present the data as code. So it will have font changes, white space will be significant, it should be skipped over by translation software and so on.

    Markup still takes effect (so you can add elements to style, or link the code to other places, and so on) so you still need to replace HTML special characters (<, &, etc) with their respective entities (&lt;, &amp;, etc).

    0 讨论(0)
  • 2020-12-19 02:58

    These tags are only for "decorational" purposes. Code within will still be executed. If you want it displayed you have to convert at least the <script> tag to html:

    &lt;script type="text/javascript"&gt;
    

    Then the JavaScript code inbetween will be shown.

    You don't need both though, I would use <pre> (which is per default a block element), <code> is intended for inline use.

    0 讨论(0)
提交回复
热议问题