Html 如何将 H1、H2 等作为链接?

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

Html how to make H1, H2, etc as links?

html

提问by Omid

What is the correct code for turning a h1, h2 etc heading into a link and search enginesindex texts of header and link both?

将 h1、h2 等标题转换search engines为标题和链接的链接和索引文本的正确代码是什么?

Is it:

是吗:

<a href="#"><h1>heading</h1></a>

or

或者

<h1><a href="#">heading</a></h1>

and could anyone explain why ?

谁能解释为什么?

回答by Williams

Per here: http://www.w3.org/TR/html401/struct/global.html#h-7.5.4

在这里:http: //www.w3.org/TR/html401/struct/global.html#h-7.5.4

%flowelement which display as a block (in this case <h1>) always should surround %inlineelements (such as <a>).

%flow显示为块的元素(在这种情况下<h1>)始终应该包围%inline元素(例如<a>)。

Another example <div>should alwaysbe outside <span>.

另一个例子<div>应该总是在外面<span>

That is to say:

也就是说:

<h1><a href="#">heading</a></h1>

is correct.

是正确的。



An even easier way of under standing this is that the following makes sense:

理解这一点的一种更简单的方法是以下内容是有道理的:

<h1><a href="#1">my link</a> and <a href="#2">my other link</a></h1>

It would be highly unusual to try the inverse with multiple <h1>s inside an <a>.

这将是极不寻常的尝试与多个逆<h1>里面一个s <a>

回答by Vinay

If You go with code1

如果你使用 code1

<a href="#"><h1>heading</h1></a>

you are just able to make it as a link with highlight feature. But If you go with code2

您只需将其作为具有突出显示功能的链接即可。但是如果你使用 code2

<h1><a href="#">heading</a></h1>

you will probably able to satisfy some css property as h1 a{ //hover or text decoration}

你可能能够满足一些 css 属性作为 h1 a{ //hover 或 text 装饰}

I will go with second code. ... Both will work.

我将使用第二个代码。......两者都会起作用。