How to print code on HTML

前端 未结 7 1755
再見小時候
再見小時候 2020-12-03 21:17

I want to display code characters on an HTML page. But no matter what I try it always renders HTML characters. pre or code doesn\'t work. How can I

相关标签:
7条回答
  • 2020-12-03 21:39
    try wrapping HTML content in <textarea></textarea> For ex: <pre> <textarea> <html> </html> </textarea> </pre> Works in awesome way. You don't have to use obsolete <XMP> example tag. <textarea> tag will wrap the content in text area. Try out ! 
    
    0 讨论(0)
  • 2020-12-03 21:40

    You need to use character references instead of the plain characters themselves:

    <code>&lt;HTML&gt;</code>
    

    The elements code and pre are just to mark the content as code or preformated.

    0 讨论(0)
  • 2020-12-03 21:43

    By escaping them.
    &amp; will print &
    &lt; will print >

    You didn't mention what you're using to generate the html, if you're manually editing, some editors have options to escape a selection. If you're using a language, look for some function that escapes html special characters. (google for how to escape html in language-name-here)

    0 讨论(0)
  • 2020-12-03 21:44

    Look for an HTML encode function in your language.

    string s = HtmlEncode(myInput);
    response.write(s)
    

    or similar

    0 讨论(0)
  • 2020-12-03 22:00

    Download tinymce From

    https://www.tinymce.com/download/

    <html>
        <head>
        <script src='tinymce/js/tinymce.min.js'></script>
        <script>
        tinymce.init({
            selector: '#myTextarea',
            height: 300,
            width:800,
            theme: 'modern',
            plugins: [
              'advlist autolink lists link image charmap print preview hr anchor pagebreak',
              'searchreplace wordcount visualblocks visualchars code fullscreen',
              'insertdatetime media nonbreaking save table contextmenu directionality',
              'emoticons template paste textcolor colorpicker textpattern imagetools'
            ],
            toolbar1: 'insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image',
            toolbar2: 'print preview media | forecolor backcolor emoticons',
            image_advtab: true
        });
        </script>
        </head>
        <body>
    
        <textarea  name="myTextarea" id="myTextarea" rows="4" cols="50">Hello Ashish</textarea>
        <input type='submit' value='save'/>
        </body>
        </html>
    
    0 讨论(0)
  • 2020-12-03 22:04

    The <xmp> tag doesn't require the contents to be escaped.

    eg:

    <xmp>
        <p>Blah &nbsp;</p>
    </xmp>
    

    ...will look like this on your screen:

        <p>Blah &nbsp;</p>
    
    0 讨论(0)
提交回复
热议问题