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

烈酒焚心 提交于 2019-11-26 06:45:00

问题


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


回答1:


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

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

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




回答2:


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

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




回答3:


An <a> tag can contain any Inline Element besides another <a> tag.




回答4:


See the anchor section of the specification.

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

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




回答5:


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 A can not be nested in another A and nesting SCRIPT doesn't make senese.




回答6:


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.

Example:

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

CSS:

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


来源:https://stackoverflow.com/questions/3379392/what-elements-can-be-contained-within-a-a-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!