html中的href属性

tech2023-02-05  112

html中的href属性

描述 (Description)

The href defines the document to which the link leads. This may be a web page in the same directory, a page somewhere else on the same server, a location within the current page, or a web page—or any another kind of document—stored on another server.

href定义链接指向的文档。 这可能是同一目录中的网页,同一服务器上其他位置的网页,当前页面中的位置,或者是另一台服务器上存储的网页(或任何其他类型的文档)。

The example shows a link between two documents in the same directory, but if the cake list document was in a directory that’s one level higher than the referencing document, the syntax would be as follows:

该示例显示了同一目录中两个文档之间的链接,但是如果蛋糕列表文档位于比引用文档高一级的目录中,则语法如下:

<a href="../cakes.html">lovely range of cakes</a>

Here, ../ equates to the instruction, “move up one directory in the hierarchy.”

在这里,.. /等于指令“在层次结构中上移一个目录”。

You can also reference a document relative to the web site’s root (the file or folder after the domain name):

您还可以引用相对于网站根目录(域名后的文件或文件夹)的文档:

<a href="/cakes.html">lovely range of cakes</a>

This link basically instructs the browser to “link to the document cakes.html that can be found in www.mydomain.com/.” This is a very handy way of referencing documents, as you could move the document containing the link to any location on the file system without breaking the link.

该链接基本上指示浏览器“链接到可在www.mydomain.com/中找到的cakes.html文档。” 这是引用文档的一种非常方便的方法,因为您可以将包含链接的文档移动到文件系统上的任何位置,而无需断开链接。

If you’re linking to a document (of whatever type) that’s held on another server, you’d express the href using a complete URI, like so:

如果要链接到另一台服务器上保存的文档(任何类型的文档),则可以使用完整的URI来表示href,如下所示:

<a href="http://www.cakebrothers.com/cakes.html">lovely range of cakes</a>

In a link to another section within the same page, the destination is identified in the href attribute by a hash symbol combined with the id attribute of the destination. This notation is known as a fragment identifier, and is shown below:

在指向同一页面内另一部分的链接中,目标是在href属性中由哈希符号与目标的id属性组合而成的。 该符号称为片段标识符,如下所示:

<!-- Here is the link --> <p>You can check the facts by reading the <a href="#refs">references at the end of this article</a></p> ⋮ <!-- Here is the link's destination --> <h3><a id="refs" href="#refs">References</a></h3>

Note these points from the example above:

请注意以上示例中的这些要点:

The href attribute is repeated in the second a element. This isn’t a mistake—it’s there because without it, Internet Explorer users who navigate to the destination would find that, although the document had moved to the correct position on the screen, the focus will not have shifted. If the user were to tab to the next link, the focus would move to the link immediately after the link the user had selected further up the page, rather than the next link after the point in the page at which the references are included. It’s possible to simply apply an id attribute to another element—for example, <h3 id="refs">References</h3>—and some browsers will allow you to activate a link and jump to that point. However, not all browsers allow this, the notable case being Internet Explorer. That’s why we need the seemingly superfluous second a element as an anchor, which is wrapped around the text inside the h3. The syntax for this simplified (but not IE-friendly) markup is shown below:

href属性在第二个a元素中重复。 这不是一个错误-在那里是因为没有它,导航到目标位置的Internet Explorer用户会发现,尽管文档已移动到屏幕上的正确位置,但是焦点不会移动。 如果用户要跳至下一个链接,则焦点将在用户选择了页面的上一个链接之后立即移至该链接,而不是页面中包含引用的点之后的下一个链接。 可以简单地将id属性应用于另一个元素,例如<h3 id="refs">References</h3> ,某些浏览器将允许您激活链接并跳至该点。 但是,并非所有浏览器都允许这样做,值得注意的是Internet Explorer。 这就是为什么我们需要看似多余的第二个元素作为锚点,并将其包裹在 h3内的文本周围 。 下面显示了这种简化的(但不是IE友好的)标记的语法:

<p>You can check the facts by reading the <a href="#refs">references at the end of this article</a></p> ⋮ <h3 id="refs">References</h3>

In your own work, you may spot examples that include both a name and id inside the anchor a element. This approach is designed to ensure that the link works in older browsers which may not allow the user to jump from one part of a document to another if the name attribute isn’t present. However, the last of the common browsers that weren’t able to link to a section of a page in this way was Netscape 4, which, thankfully, is now almost completely obsolete. Also, note that in HTML 5 the name attribute has been removed, so it’s a good idea to break the habit of using name.

在您自己的作品中,您可能会发现在元素的锚点中同时包含名称和ID的示例。 此方法旨在确保链接在较旧的浏览器中有效,如果没有name属性,则该链接可能不允许用户从文档的一部分跳到另一部分。 但是,最后无法以这种方式链接到页面部分的常见浏览器是Netscape 4,值得庆幸的是,现在它已几乎完全过时了。 另外,请注意,在HTML 5中, name属性已被删除,因此,最好改掉使用name的习惯。

In addition to links to documents (such as web pages or other document types), the href attribute may specify a different protocol, including:

除了指向文档(例如网页或其他文档类型)的链接之外,href属性还可以指定其他协议,包括:

ftp typed as <a href="ftp://someftpserver.com/">Browse the FTP server</a>, which will open a connection to an FTP server

FTP类型为<a href="ftp://someftpserver.com/">Browse the FTP server</a> ,这将打开到FTP服务器的连接

mailto typed as <a href="mailto:mrwhatever@somedomain.com">Email me!</a>, which will trigger an email client to open and create a new message whose To address matches whatever appears after the mailto: protocol (in this case, an email to mrwhatever@somedomain.com)

mailto键入为<a href="mailto:mrwhatever@somedomain.com">Email me!</a>发送<a href="mailto:mrwhatever@somedomain.com">Email me!</a> ,这将触发电子邮件客户端打开并创建新邮件,该邮件的“收件人地址”与mailto:协议后出现的内容相符(在这种情况下,请发送电子邮件至mrwhatever@somedomain.com)

(Value)

This attribute takes as its value the location of the destination document relative to the referencing document, relative to the server root, or in the form of a complete URI containing the http:// protocol, the server name, and the path to the document on that server. It may also contain a reference to the ftp:// or mailto: protocols.

此属性将目标文档相对于参考文档,相对于服务器根目录的位置或包含http://协议,服务器名称和文档路径的完整URI的形式作为其值。在该服务器上。 它还可能包含对ftp://或mailto:协议的引用。

翻译自: https://www.sitepoint.com/href-html-attribute/

html中的href属性

最新回复(0)