TOC

The community is working on translating this tutorial into Chinese, but it seems that no one has started the translation process for this article yet. If you can help us, then please click "More info".

HTML Basics:

Links

A link, or an anchor, is a word, a group of words or an image you can click to jump to another document or a specific part of the current document. The element for links, both internal and external is as simple as <a>. I’ve talked about abbreviations before and a is an abbreviation of anchor. But you cannot just use <a> - you have to add at least one attribute – the href. Href stand for hypertext reference and the value defines the address you are linking to. The simplest link looks like this:

<a href="http://www.yoururl.com">Link text</a>

Open in a new window

有时候,您希望用户继续打开着您的网页,所以您可以确定应不应该在新窗口中打开新的链接。为此,您可以使用目标属性。

If you decide to use the target attribute, you should be aware that a lot of people dislike this feature and believes that the user should be allowed to decide for herself whether or not she wants a new window, by right-clicking. Having said this, the attribute/value for opening in a new window looks like this:

<a href="http://www.yoururl.com" target="_blank">This is my link</a>

Link to a specific part of your document

If you want to link to a specific part of your document, this can be done in two ways. Previously, it was done by identifying the place you wanted to link to, an name this place – that would look like this:

<a name="question-2">This</a> is the answer to the second question

This is called a fragment identifier – it identifies a specific fragment of you document. The anchor-element is used to define the place you want to link to and you would also use the fragment identifier when you actually linked to it:

<a href="#question-2">See the answer to question 2</a>

The # indicates that it is a fragment identifier link and the question-2 tells the browser which fragment to look for.

HTML5 suggests using specific id-attributes instead of the name-attribute if they are already available and if they are not, to writ the anchor element a bit different.

以下是使用id的例子。

<p id="my-note">This is my first section, with a unique id</p>
<p>This is just a section – no id’s or anything here</p>

<a href="#my-note">Go to the first section</a>

如果在这个段落里,没有id,但是我们还想把他连接到第一部分,HTML5认为您因该这样:

<p><a name="my-note" />This is my first section, with a unique id</p>
<p>This is just a section – no id’s or anything here</p>

<a href="#my-note">Go to the first section</a>

As you can see, we use the anchor-element if there is no unique id and then close the element in the first tag, as the closing tag is not needed.


This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!
adplus-dvertising