如何在 HTML 上打印代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1756164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 01:21:35  来源:igfitidea点击:

How to print code on HTML

html

提问by paloturk

I want to display code characters on an HTML page. But no matter what I try it always renders HTML characters. preor codedoesn't work. How can I do this?

我想在 HTML 页面上显示代码字符。但无论我尝试什么,它总是呈现 HTML 字符。precode不起作用。我怎样才能做到这一点?

回答by nickf

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

<xmp>不需要标签的内容进行转义。

eg:

例如:

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

...will look like this on your screen:

...将在您的屏幕上看起来像这样:

    <p>Blah &nbsp;</p>

回答by Gumbo

You need to use character referencesinstead of the plain characters themselves:

您需要使用字符引用而不是纯字符本身:

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

The elements codeand preare just to mark the content as codeor preformated.

元素codepre只是将内容标记为代码预先格式化

回答by Prody

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

通过逃避他们。
&amp;会打印&
&lt;会打印>

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)

你没有提到你用来生成 html 的内容,如果你手动编辑,一些编辑器可以选择转义选择。如果您使用的是一种语言,请寻找一些可以转义 html 特殊字符的函数。(谷歌如何在 中转义 html language-name-here

回答by No Refunds No Returns

Look for an HTML encode function in your language.

寻找您的语言中的 HTML 编码功能。

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

or similar

或类似

回答by weeger

Since tag is now depreciated : https://developer.mozilla.org/en-US/docs/Web/HTML/Element/xmp

由于标签现已折旧:https: //developer.mozilla.org/en-US/docs/Web/HTML/Element/xmp

You can use PHP to convert HTML tags :

您可以使用 PHP 来转换 HTML 标签:

<code> <?php print htmlentities('<p>Blah</p>'); ?> </code>

<code> <?php print htmlentities('<p>Blah</p>'); ?> </code>

回答by Ashish pathak

Download tinymce From

下载tinymce来自

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

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>

回答by nisargdave1993

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 !