如何在 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
How to print code on HTML
提问by paloturk
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 do this?
我想在 HTML 页面上显示代码字符。但无论我尝试什么,它总是呈现 HTML 字符。pre
或code
不起作用。我怎样才能做到这一点?
回答by nickf
The <xmp>
tag doesn't require the contents to be escaped.
在<xmp>
不需要标签的内容进行转义。
eg:
例如:
<xmp>
<p>Blah </p>
</xmp>
...will look like this on your screen:
...将在您的屏幕上看起来像这样:
<p>Blah </p>
回答by Gumbo
You need to use character referencesinstead of the plain characters themselves:
您需要使用字符引用而不是纯字符本身:
<code><HTML></code>
The elements code
and pre
are just to mark the content as codeor preformated.
元素code
和pre
只是将内容标记为代码或预先格式化。
回答by Prody
By escaping them.&
will print &
<
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
)
你没有提到你用来生成 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 !