Html 标题内的锚点,反之亦然 - 与 SEO 有什么不同吗?

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

Anchors inside headers, or vice versa - is there a different in relation to SEO?

htmlhyperlinkseosemantics

提问by Frederik Wordenskjold

I always stumble upon this (rather simple question) when making e.g. Wordpress blogs. Should I wrap the headline, which should also be a link, in a anchor <a>element, or should it be the other way around. And why?

在制作 Wordpress 博客时,我总是偶然发现这个(相当简单的问题)。我应该将标题(也应该是链接)包装在锚<a>元素中,还是应该反过来。为什么?

(1):

(1):

<a href="foo">
    <h1>bar</h1>
</a>

(2):

(2):

<h1>
    <a href="foo">bar</a>
</h1>

I always end up doing (1), as it just seems more logical to me, that the headline is wrapped in a link.

我总是最终做(1),因为对我来说似乎更合乎逻辑,标题包含在链接中。

Does it make any difference in e.g. SEO? Will it reflect the SEO ranking?

它对例如 SEO 有什么影响吗?它会反映SEO排名吗?

采纳答案by animuson

Prior to HTML5:
The anchor has to be inside the header, you cannot put a block-level element inside an anchor, and most browsers will not render it 100% reliably if you do.

在 HTML5 之前:
锚点必须位于标题内,您不能将块级元素放在锚点内,如果您这样做,大多数浏览器将无法 100% 可靠地呈现它。

In HTML5:
It doesn't matter, use whichever one makes the most semanticsense. Likely the first one.

在 HTML5 中:
没关系,使用最符合语义的那个。应该是第一个。

Remember that if your document is using HTML4 DTD's, it will not validate and may not render correctly because it's using the old rules where an anchor cannot contain a block-level element. Only use the first option in HTML5. XHTML is equivalent to HTML4, I'm not 100% sure about XHTML1.1 though (try it and see if it validates).

请记住,如果您的文档使用 HTML4 DTD,它将无法验证并且可能无法正确呈现,因为它使用旧规则,其中锚点不能包含块级元素。仅使用 HTML5 中的第一个选项。XHTML 等同于 HTML4,不过我不是 100% 确定 XHTML1.1(尝试一下,看看它是否有效)。

回答by MGZero

If you're using an HTML 4.01 DTD (Not sure about the transitional, but definitely the strict), then the anchor element must appear inside the header element. Check your doctype if you don't know which DTD you're using (assuming you have one, which you do need for oyur code to be considered valid). The HTML5 doctype looks like this:

如果您使用的是 HTML 4.01 DTD(不确定过渡,但绝对是严格的),则锚元素必须出现在标题元素内。如果您不知道您使用的是哪个 DTD,请检查您的 doctype(假设您有一个,您确实需要它才能将 oyur 代码视为有效)。HTML5 文档类型如下所示:

<!DOCTYPE HTML>

The 4.01 doctype is:

4.01 文档类型是:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 

I'd also suggest familiarizing yourself with the DTD itself if you're going to work with 4.01, it will answer all of these types of questions.

如果您打算使用 4.01,我还建议您熟悉 DTD 本身,它将回答所有这些类型的问题。