Html 在 <a> 标签中添加 <span> 标签是否正确?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7427177/
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
Is it right to add <span> tag inside <a> tag?
提问by Codeobsession
I was wondering if we could add a span tag inside a tag....
我想知道我们是否可以在标签中添加一个 span 标签....
<h2 style="text-align: left;"><a href="mydomain.com"><span style="font-weight: bold;">Dog dresses</span></a><br></h2>
Is the above method right? Is it safe to use...???
上面的方法对吗?使用安全吗……???
I know that We can define some class here and let it go inside other elements like this.
我知道我们可以在这里定义一些类,然后让它进入像这样的其他元素。
<h2><a href="mydomain" class="bold">Dog dresses</a></h2>
.bold {font-weight: bold; }
OR
或者
<h2 class="bold"><a href="mydomain">Dog dresses</a></h2>
h2.bold a { font-weight: bold; }
Please share your views..
请分享您的看法..
回答by Paul
Yes, it's fine. <span>
is an inline element. Unless you add the css display: block;
to it, it can go in the <a>
.
是的,没关系。<span>
是一个内联元素。除非您向其中添加 css display: block;
,否则它可以放在<a>
.
回答by inspite
Both forms are legal. (<a>
inside <span>
or <span>
inside <a/>
)
两种形式都是合法的。(<a>
内部<span>
或<span>
内部<a/>
)
<a><div></div></a>
<!-- illegal < HTML 5, you cannot put block level tags in an <a> -->
<!-- legal in HTML 5 -->
BUT, normally I would only use a <span>
inside an <a>
for some purpose, because there is some content which needs special treatment
但是,通常我只会出于某种目的使用<span>
内部<a>
,因为有些内容需要特殊处理
<a href="#">this is <span class="lookatme">special and needs treatment</span></a>
This is pointless (for me :-) )
这是毫无意义的(对我来说:-))
<a href="#"><span class="lookatme">some text</span></a>
THis would normally be
这通常是
<a href="#" class="lookatme">some text</a>
I normally think with <heading>
tags, the <a>
should be inside the <heading>
, but I don't think it is wrong to do the reverse
我通常认为带<heading>
标签,<a>
应该在 里面<heading>
,但我不认为做相反的事情是错误的
回答by Eric
While that code isvalid, it's not the best way to do it.
虽然该代码是有效的,但它并不是最好的方法。
Here's your code again, indented for clarity
这是你的代码,为了清楚起见缩进
<h2 style="text-align: left;">
<a href="mydomain.com">
<span style="font-weight: bold;">Dog dresses</span>
</a>
<br>
</h2>
The first thing to notice is you have a trailing <br>
. What's that for? Extra spacing? Well use padding
instead!
首先要注意的是您有一个尾随<br>
. 那个有什么用途?额外的间距?用padding
吧!
Secondly, you don't need the span element - the bold style can be applied directly to the <a>
tag.
其次,您不需要 span 元素——粗体样式可以直接应用于<a>
标签。
Why not just write it like this:
为什么不这样写:
<h2 style="text-align: left; padding-bottom: 1em">
<a href="http://mydomain.com" style="font-weight: bold">Dog dresses</a>
</h2>
回答by a.b
It is legal and safe. You can always check your markup at free validation service of w3 organisation: http://validator.w3.org/check
这是合法且安全的。您可以随时在 w3 组织的免费验证服务中检查您的标记:http: //validator.w3.org/check
回答by brain
It's perfectly legal to have a span
tag inside an a
tag.
在span
标签中放置a
标签是完全合法的。
Also read this:
另请阅读:
回答by mpen
If memory serves correctly, yes, you're allowed to have a <span>
within an <a>
, and an <a>
within an <h2>
. Where you define your class is up to you; put it wherever it makes most sense.
如果记错,是的,你被允许有一个<span>
内<a>
和<a>
内<h2>
。你在哪里定义你的类取决于你;把它放在最有意义的地方。
You can check if you've written valid HTML here, but don't fret too much if it doesn't validate as long as it renders correctly in all the prominent browsers.
您可以在此处检查您是否编写了有效的 HTML ,但如果它没有通过验证,请不要太担心,只要它在所有主流浏览器中都能正确呈现。
回答by Rakesh
I would perfer to use CSS rather then using inside . It reduce the complexity of HTML code, it reduce the stress to the browser, by not rendering complex structure. Easy to grab using JavaScript.
我宁愿使用 CSS 而不是使用 inside 。它通过不渲染复杂的结构来降低 HTML 代码的复杂性,减少对浏览器的压力。使用 JavaScript 易于抓取。
回答by ErTR
I want to add a different perspective which lacks among answers. Imagine you want to achive something like this, partially linked header:
我想补充一个不同的观点,这是答案中缺乏的。想象一下,你想要实现这样的东西,部分链接的标题:
<h1><a href="stackoverflow.com">This</a> site is amazing</h1>
and a link which is a partial header:
以及作为部分标题的链接:
<a href="stackoverflow.com"><h1>This</h1> site is amazing</a>
which makes not much sense but syntactically true.
这没有多大意义,但在语法上是正确的。