如何在 HTML 中复制 \t 制表符空间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20414504/
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-28 22:22:34  来源:igfitidea点击:

How do I replicate a \t tab space in HTML?

htmlhtml-escape-characters

提问by user2443085

How can I use the common \t escape character in html ? Is it possible?

如何在 html 中使用常见的 \t 转义字符?是否可以?

I need a code that has the same function as the /t escape character

我需要一个与 /t 转义字符具有相同功能的代码

采纳答案by Pekka

I need a code that has the same function as the /t escape character

我需要一个与 /t 转义字符具有相同功能的代码

What function do you mean, creating a tabulator space?

你的意思是什么功能,创建一个制表符空间?

No such thing in HTML, you'll have to use HTML elements for that. (A <table>may make sense for tabular data, or a description list <dl>for definitions.)

HTML 中没有这样的东西,您必须为此使用 HTML 元素。(A<table>对表格数据或定义的描述列表<dl>可能有意义。)

回答by Jukka K. Korpela

You can enter the tab character (U+0009 CHARACTER TABULATION, commonly known as TAB or HT) using the character reference &#9;. It is equivalent to the tab character as such. Thus, from the HTML viewpoint, there is no need to “escape” it using the character reference; but you may do so e.g. if your editing program does not let you enter the character conveniently.

您可以使用字符引用输入制表符(U+0009 CHARACTER TABULATION,通常称为 TAB 或 HT)&#9;。它等同于制表符本身。因此,从 HTML 的角度来看,没有必要使用字符引用来“转义”它;但是您可以这样做,例如,如果您的编辑程序不允许您方便地输入字符。

On the other hand, the tab character is in most contexts equivalent to a normal space in HTML. It does not “tabulate”, it's just a word space.

另一方面,制表符在大多数情况下相当于 HTML 中的普通空格。它不是“制表”,它只是一个词空间。

The tab character has, however, special handling in preelements and (although this not that well described in specifications) in textareaand xmpelement (in the latter, character references cannot be used, only the tab character as such). This is described somewhat misleadingly in HTML specifications, e.g. in HTML 4.01: “[Inside the preelement, ] the horizontal tab character (decimal 9 in [ISO10646] and [ISO88591] ) is usually interpreted by visual user agents as the smallest non-zero number of spaces necessary to line characters up along tab stops that are every 8 characters. We strongly discourage using horizontal tabs in preformatted text since it is common practice, when editing, to set the tab-spacing to other values, leading to misaligned documents.”

然而,制表符在pre元素和(尽管在规范中没有很好描述)textareaxmp元素(在后者中,不能使用字符引用,只有制表符本身)中有特殊处理。这在 HTML 规范中有些误导,例如在 HTML 4.01 中:“[在pre元素内部,] 水平制表符([ISO10646] 和 [ISO88591] 中的十进制 9)通常被可视化用户代理解释为最小的非零沿着每 8 个字符的制表位排列字符所需的空格数。我们强烈建议不要在预格式化的文本中使用水平制表符,因为在编辑时将制表符间距设置为其他值是常见的做法,这会导致文档未对齐。”

The warnings are unnecessary except as regards to the potential mismatch of tabbing in your authoring software and HTML rendering in browsers. The real reason for avoiding horizontal tab is that it a coarse and simplistic tool as compared with tables for presenting tabular material. And in displaying computer source programs, it is better to use just spaces inside pre, since the default tab stops at every 8 characters are quite unsuitable for any normal code indentation style.

这些警告是不必要的,除非您的创作软件中的 Tab 键可能与浏览器中的 HTML 呈现不匹配。避免使用水平制表符的真正原因是,与用于呈现表格材料的表格相比,它是一种粗糙而简单的工具。并且在显示计算机源程序时,最好只在里面使用空格pre,因为每 8 个字符的默认制表符停止位对于任何正常的代码缩进样式都非常不合适。

In addition, in CSS, you can specify white-space: pre(or, with slightly more limited browser support, white-space: pre-wrap) to make a normal HTML element, like divor p, rendered like pre, so that all whitespace is preserved and horizontal tab has the “tabbing” effect.

此外,在 CSS 中,您可以指定white-space: pre(或者,在浏览器支持稍微有限的情况下,white-space: pre-wrap)制作一个普通的 HTML 元素,如divp,呈现为pre,以便保留所有空格,并且水平制表符具有“制表”效果。

In CSS Text Module Level 3 (Last Call working draft, i.e. proceeding towards maturity), there is also the tab-sizeproperty, which can be used to set the distance between tab stops, e.g. tab-size: 3. It's supportedby newest versions of most browsers, but not IE (not even IE 11).

在 CSS Text Module Level 3(Last Call 工作草案,即走向成熟)中,还有一个tab-size属性,可用于设置制表位之间的距离,例如tab-size: 3. 大多数浏览器的最新版本都支持它,但不支持 IE(甚至不支持 IE 11)。

回答by xr280xr

&nbsp;&nbsp;&nbsp;&nbsp;would be a work around if you're only after the spacing.

&nbsp;&nbsp;&nbsp;&nbsp;如果您只是在间距之后,这将是一种解决方法。

回答by Dai

HTML doesn't have escape characters (as it doesn't use escape-semantics for reserved characters, instead you use SGML entities: &amp;, &lt;, &gt;and &quot;).

HTML没有转义字符(因为它不使用转义的语义学保留字符,而不是你使用SGML实体:&amp;&lt;&gt;&quot;)。

SGML does not have a named-entity for the tab character as it exists in most character sets (i.e. 0x09in ASCII and UTF-8), rendering it completely unnecessary (i.e. simply press the Tabkey on your keyboard). If you're working with code that generates HTML (e.g. a server-side application, e.g. ASP.NET, PHP or Perl, then you might need to escape it then, but only because the server-side language demands it - it has nothing to do with HTML, like so:

SGML 没有制表符的命名实体,因为它存在于大多数字符集中(即0x09在 ASCII 和 UTF-8 中),因此完全没有必要(即只需按下Tab键盘上的键)。如果您正在使用生成 HTML 的代码(例如服务器端应用程序,例如 ASP.NET、PHP 或 Perl,那么您可能需要转义它,但这只是因为服务器端语言需要它 - 它没有任何东西)与 HTML 相关,如下所示:

echo "<pre>\t\tTABS!\t\t</pre>";

But, you can use SGML entities to represent any ISO-8859-1 character by hexadecimal value, e.g. &#09;for a tab character.

但是,您可以使用 SGML 实体以十六进制值表示任何 ISO-8859-1 字符,例如&#09;制表符。

回答by Andrew Puglionesi

This simple formula should work. Give the element whose text will contain a tab the following CSS property: white-space:pre. Otherwise your html may not render tabs at all. Then, wherever you want to have a tab in your text, type &#9;.

这个简单的公式应该有效。为文本将包含选项卡的元素提供以下 CSS 属性: white-space:pre. 否则您的 html 可能根本无法呈现选项卡。然后,无论您想在文本中的哪个位置添加选项卡,请键入&#9;

Since you didn't mention CSS, if you want to do this without a CSS file, just use

既然你没有提到 CSS,如果你想在没有 CSS 文件的情况下做到这一点,只需使用

<tag-name style="white-space:pre">text in element&#9;more text</tag-name>

in your HTML.

在您的 HTML 中。

回答by CatMan

The same issue exists for a Mediawiki: It does not provide tabs, nor are consecutive spaces allowed.

Mediawiki 也存在同样的问题:它不提供制表符,也不允许连续空格。

Although not really a TAB function, the workaround was to add a template named 'Tab', which replaces each call (i.e. {{tab}}) by 4 non-breaking space symbols:

虽然不是真正的 TAB 函数,但解决方法是添加一个名为“Tab”的模板,它将每个调用(即{{tab}})替换为 4 个不间断空格符号:

 &nbsp;&nbsp;&nbsp;&nbsp; 

Those are not collapsed, and create a 4 space distance anywhere used.
It's not really a tab, because it would not align to fixed tab positions, but I still find many uses for it.

这些不会折叠,并在任何使用的地方创建 4 空间距离。
它不是真正的选项卡,因为它不会与固定的选项卡位置对齐,但我仍然发现它有很多用途。

Maybe someone can come up with similar mechanism for a Wiki Template in HTML (CSS class or whatever).

也许有人可以为 HTML 中的 Wiki 模板(CSS 类或其他)提出类似的机制。

回答by Giampaolo Ficorilli

You can use this &emsp;or &ensp;

你可以使用这个 &emsp;&ensp;

It works on Visual Studio

它适用于 Visual Studio

回答by hizlan erpak

You must use

你必须使用

<dd> </dd>in the html code.

<dd> </dd>在 html 代码中。

<dd>A free, open source, cross-platform,graphical web browser developed by theMozilla Corporation and hundreds of volunteers.</dd>

----------------------------------
Firefox
    A free, open source, cross-platform, graphical web browser developed by the Mozilla 
    Corporation and hundreds of volunteers.

enter image description here

在此处输入图片说明