锚内部的标题,或反之亦然-是否有一个不同的关系搜索引擎优化?

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

(1) :

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

(2) :

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

我总是最后做 (1),因为它似乎更符合逻辑,对我来说,标题是包装在一个链接。

在搜索引擎优化方面有什么不同吗? 它会反映搜索引擎优化的排名吗?

25364 次浏览

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.

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

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).

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:

<!DOCTYPE HTML>

The 4.01 doctype is:

<!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.

First one is best for the SEO because google index that very easily.