Html <a> 标签中可以包含哪些元素?

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

What elements can be contained within a <a> tag?

htmlvalidationhtml4

提问by Ahmad

What are the valid html elements, if any, that can be contained within a <a>tag?

<a>标签中可以包含哪些有效的 html 元素(如果有)?

采纳答案by meder omuraliev

Inline elements ( a, span, strong, emamong others ) can contain other inline elements and text nodes. An anchor can contain a span, which can contain a text node.

内联元素(a, span, strong, em除其他外)可以包含其他内联元素和文本节点。锚点可以包含跨度,跨度可以包含文本节点。

Generally, block-level elements may contain inline elements and other block-level elements. Generally, inline elements may contain only data and other inline elements. Inherent in this structural distinction is the idea that block elements create "larger" structures than inline elements.

通常,块级元素可能包含行内元素和其他块级元素。通常,内联元素可能只包含数据和其他内联元素。这种结构区别的固有思想是块元素创建比行内元素“更大”的结构。

From http://www.w3.org/TR/html401/struct/global.html

来自http://www.w3.org/TR/html401/struct/global.html

As noted in other answers, you can't nest an ain an a.

正如其他答案中所述,您不能将aan嵌套在a.

回答by Aya

As of HTML 5, <a>may contain not only (valid) inline elements, but also block elements, etc.

从 HTML 5 开始,<a>不仅可以包含(有效的)内联元素,还可以包含块元素等。

W3: http://dev.w3.org/html5/markup/a.html

W3:http: //dev.w3.org/html5/markup/a.html

回答by Mike Sherov

An <a>tag can contain any Inline Elementbesides another <a>tag.

一个<a>标签可以包含除另一个标签之外的任何内联元素<a>

回答by Quentin

See the anchor sectionof the specification.

请参阅规范的锚定部分

<!ELEMENT A - - (%inline;)* -(A)       -- anchor -->

The relevant section is (%inline;)* -(A), which means "Anything in the group %inline excluding A elements". %inlineis hyperlinked to make it easier for you to expand it.

相关部分是(%inline;)* -(A),意思是“%inline 组中不包括 A 元素的任何内容”。%inline是超链接的,可以让您更轻松地展开它。

回答by Guffa

An anchor tag is an inline element, so it can contain other inline elements (except other anchor tags).

锚标记是内联元素,因此它可以包含其他内联元素(其他锚标记除外)。

If you want to put a block element inside an anchor, you have to use an inline element and turn it into a block element using CSS, along with the anchor tag itself.

如果你想把一个块元素放在一个锚中,你必须使用一个内联元素,并使用 CSS 把它和锚标签本身一起变成一个块元素。

Example:

例子:

<a href="page.html" class="blocklink"><span>eat me</span></a>

CSS:

CSS:

.blocklink { display: block; }
.blocklink span { display: block; }

回答by Ale? Roubí?ek

It can contain plain text and inline elements. Inline elements are following:

它可以包含纯文本和内联元素。内联元素如下:

TT | I | B | BIG | SMALL | EM | STRONG | DFN | CODE | SAMP | 
KBD | VAR | CITE | ABBR | ACRONYM | A | IMG | OBJECT | BR | 
SCRIPT | MAP | Q | SUB | SUP | SPAN | BDO

But Acan not be nested in another Aand nesting SCRIPTdoesn't make senese.

A不能嵌套在另一个中A,嵌套SCRIPT也没有意义。