I want to nest span
and a
tags. Should I
我想要嵌套span和一个标签。我应该
- Put
<span>
inside<a>
- 把< span >在 <一>
- Put
<a>
inside<span>
- 将< > < span >内
- It doesn't matter ?
- 没关系吗?
10 个解决方案
#1
98
3 - It doesn't matter.
3 -没关系。
BUT, I tend to only use a <span>
inside an <a>
if it's only for a part of the contents of the tag i.e.
但是,我倾向于只在中使用,如果它仅用于标记的一部分内容,即。
<a href="#">some <span class="red">text</span></a>
Rather than:
而不是:
<a href="#"><span class="red">some text</span></a>
Which should obviously just be:
显然应该是:
<a href="#" class="red">some text</a>
#2
30
It is perfectly valid (at least by HTML 4.01 and XHTML 1.0 standards) to nest either a <span>
inside an <a>
or an <a>
inside a <span>
.
将嵌在中或嵌在中是完全有效的(至少在HTML 4.01和XHTML 1.0标准中是如此)。
Just to prove it to yourself, you can always check it out an the W3C MarkUp Validation Service
为了向您自己证明,您总是可以查看W3C标记验证服务
I tried validating:
我试着验证:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Title</title>
</head>
<body>
<p>
<a href="http://www.google.com/"><span>Google</span></a>
</p>
</body>
</html>
And also the same as above, but with the <a>
inside the <span>
和上面一样,但是在
i.e.
即。
<span><a href="http://www.google.com">Google</a></span>
with both HTML 4.01 and XHTML 1.0 doctypes, and both passed validation successfully!
使用HTML 4.01和XHTML 1.0 doctype,并且都成功通过了验证!
Only thing to be aware of is to ensure that you close the tags in the correct order. So if you start with a <span>
then an <a>
, make sure you close the <a>
tag first before closing the <span>
and vice-versa.
唯一需要注意的是确保您以正确的顺序关闭标签。因此,如果您从开始,然后是,请确保在关闭之前先关闭标记,反之亦然。
#3
17
It doesn't matter - they're both allowed inside each other.
没关系——他们都被允许在一起。
#4
16
that depends on what you want to markup.
这取决于您想要标记什么。
- if you want a link inside a span, put
<a>
inside<span>
. - 如果你想在一个范围内建立一个链接,把放在内。
- if you want to markup something in a link, put
<span>
into<a>
- 如果您想在链接中标记某些内容,请将放入
#5
15
SPAN is a GENERIC inline container. It does not matter whether an a
is inside span
or span
is inside a
as both are inline elements. Feel free to do whatever seems logically correct to you.
SPAN是一个通用的内联容器。a是在span内还是span在a内都没有关系,因为两者都是内联元素。你可以*地做任何看起来合乎逻辑的事情。
#6
2
It depends on what the span is for. If it refers to the text of the link, and not the fact that it is a link, choose #1. If the span refers to the link as a whole, then choose #2. Unless you explain what the span represents, there's not much more of an answer than that. They're both inline elements, can be syntactically nested in any order.
这取决于张成的空间。如果它指向链接的文本,而不是链接的事实,请选择#1。如果span表示整个链接,那么选择#2。除非你解释张成的空间代表什么,否则没有比这个答案更多的答案了。它们都是内联元素,可以按任何顺序进行语法嵌套。
#7
1
Personally, as a web developer, I only ever put a span within an anchor tag if I am trying to highlight a section of the links text, such as applying a background to one section.
就我个人而言,作为一名web开发人员,如果我想突出显示链接文本的某个部分,比如将背景应用到某个部分,我只会在锚标记中放置一个span。
#8
1
It can matter if for instance you are using some sort icon font. I had this just now with:
例如,如果您正在使用某种排序图标字体,这可能会很重要。我刚才说:
<span class="fa fa-print fa-3x"><a href="some_link"></a></span>
Normally I would put the span inside the A but the styling wasn't taking effect until swapped it round.
通常我会把跨度放在A里面,但是直到把它调换一下,样式才生效。
#9
0
It will work both, but personally I'd prefer option 2 so the span is "around" the link.
它会同时工作,但我个人更喜欢选项2,所以跨度是“围绕”这个链接。
#10
0
Semantically I think makes more sense as is a container for a single element and if you need to nest them then that suggests more than element will be inside of the outer one.
从语义上讲,我认为更有意义的是一个单一元素的容器,如果你需要嵌套它们,那就意味着,比元素更多的是在外部。
#1
98
3 - It doesn't matter.
3 -没关系。
BUT, I tend to only use a <span>
inside an <a>
if it's only for a part of the contents of the tag i.e.
但是,我倾向于只在中使用,如果它仅用于标记的一部分内容,即。
<a href="#">some <span class="red">text</span></a>
Rather than:
而不是:
<a href="#"><span class="red">some text</span></a>
Which should obviously just be:
显然应该是:
<a href="#" class="red">some text</a>
#2
30
It is perfectly valid (at least by HTML 4.01 and XHTML 1.0 standards) to nest either a <span>
inside an <a>
or an <a>
inside a <span>
.
将嵌在中或嵌在中是完全有效的(至少在HTML 4.01和XHTML 1.0标准中是如此)。
Just to prove it to yourself, you can always check it out an the W3C MarkUp Validation Service
为了向您自己证明,您总是可以查看W3C标记验证服务
I tried validating:
我试着验证:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Title</title>
</head>
<body>
<p>
<a href="http://www.google.com/"><span>Google</span></a>
</p>
</body>
</html>
And also the same as above, but with the <a>
inside the <span>
和上面一样,但是在
i.e.
即。
<span><a href="http://www.google.com">Google</a></span>
with both HTML 4.01 and XHTML 1.0 doctypes, and both passed validation successfully!
使用HTML 4.01和XHTML 1.0 doctype,并且都成功通过了验证!
Only thing to be aware of is to ensure that you close the tags in the correct order. So if you start with a <span>
then an <a>
, make sure you close the <a>
tag first before closing the <span>
and vice-versa.
唯一需要注意的是确保您以正确的顺序关闭标签。因此,如果您从开始,然后是,请确保在关闭之前先关闭标记,反之亦然。
#3
17
It doesn't matter - they're both allowed inside each other.
没关系——他们都被允许在一起。
#4
16
that depends on what you want to markup.
这取决于您想要标记什么。
- if you want a link inside a span, put
<a>
inside<span>
. - 如果你想在一个范围内建立一个链接,把放在内。
- if you want to markup something in a link, put
<span>
into<a>
- 如果您想在链接中标记某些内容,请将放入
#5
15
SPAN is a GENERIC inline container. It does not matter whether an a
is inside span
or span
is inside a
as both are inline elements. Feel free to do whatever seems logically correct to you.
SPAN是一个通用的内联容器。a是在span内还是span在a内都没有关系,因为两者都是内联元素。你可以*地做任何看起来合乎逻辑的事情。
#6
2
It depends on what the span is for. If it refers to the text of the link, and not the fact that it is a link, choose #1. If the span refers to the link as a whole, then choose #2. Unless you explain what the span represents, there's not much more of an answer than that. They're both inline elements, can be syntactically nested in any order.
这取决于张成的空间。如果它指向链接的文本,而不是链接的事实,请选择#1。如果span表示整个链接,那么选择#2。除非你解释张成的空间代表什么,否则没有比这个答案更多的答案了。它们都是内联元素,可以按任何顺序进行语法嵌套。
#7
1
Personally, as a web developer, I only ever put a span within an anchor tag if I am trying to highlight a section of the links text, such as applying a background to one section.
就我个人而言,作为一名web开发人员,如果我想突出显示链接文本的某个部分,比如将背景应用到某个部分,我只会在锚标记中放置一个span。
#8
1
It can matter if for instance you are using some sort icon font. I had this just now with:
例如,如果您正在使用某种排序图标字体,这可能会很重要。我刚才说:
<span class="fa fa-print fa-3x"><a href="some_link"></a></span>
Normally I would put the span inside the A but the styling wasn't taking effect until swapped it round.
通常我会把跨度放在A里面,但是直到把它调换一下,样式才生效。
#9
0
It will work both, but personally I'd prefer option 2 so the span is "around" the link.
它会同时工作,但我个人更喜欢选项2,所以跨度是“围绕”这个链接。
#10
0
Semantically I think makes more sense as is a container for a single element and if you need to nest them then that suggests more than element will be inside of the outer one.
从语义上讲,我认为更有意义的是一个单一元素的容器,如果你需要嵌套它们,那就意味着,比元素更多的是在外部。