Home >Web Front-end >HTML Tutorial >HTML target attribute

HTML target attribute

高洛峰
高洛峰Original
2017-02-16 14:25:222342browse

Definition and usage

## The target attribute of the tag specifies where to open the linked document.

If you include a target attribute within an
tag, the browser will load and display the document in the frame or window named with the tag's href attribute and whose name matches the target. If the frame or window with the specified name or id does not exist, the browser will open a new window, give the window the specified tag, and then load the new document into that window. From now on, hyperlinked documents can point to this new window.

Open New Window

Pointed hyperlinks make it easy to create efficient browsing tools. For example, a simple list of content documents can redirect the document to a separate window:

<h3>Table of Contents</h3>
<ul>
  <li><a href="pref.html" target="view_window">Preface</a></li>
  <li><a href="chap1.html" target="view_window">Chapter 1</a></li>
  <li><a href="chap2.html" target="view_window">Chapter 2</a></li>
  <li><a href="chap3.html" target="view_window">Chapter 3</a></li>
</ul>
Try it yourself

When the user first selects an item in the content list When linking, the browser will open a new window, label it "view_window", and display the desired document content there. If the user selects another link from this content list and the "view_window" is still open, the browser will load the selected document into that window again, replacing the previous documents.

Throughout the entire process, this window containing the content list is accessible to the user. By clicking a link in a window, the contents of another window can be changed.

Open a window in a frame

Instead of opening a full browser window, the more common method of using target is to direct the hyperlink content to one or more objects in a display. in a frame. You can put this content list into one frame of a two-frame document and use the adjacent frame to display the selected document:

<frameset cols="100,*">
  <frame src="toc.html">
  <frame src="pref.html" name="view_frame">
</frameset>
Try it yourself

When the browser initially displays these two frames, the frame on the left contains the table of contents, and the frame on the right contains the preface.

This is the source code of "toc.html":

<h3>Table of Contents</h3>
<ul>
  <li><a href="pref.html" target="view_frame">Preface</a></li>
  <li><a href="chap1.html" target="view_frame">Chapter 1</a></li>
  <li><a href="chap2.html" target="view_frame">Chapter 2</a></li>
  <li><a href="chap3.html" target="view_frame">Chapter 3</a></li>
</ul>
Please note that in the document "toc.html", the target of each link is "view_frame", which is the right side s frame.

When the user selects a link from the directory in the left frame, the browser will load the associated document and display it in the "view_frame" frame on the right. When other links are selected, the content in the right frame will also change, while the left frame will always remain the same.

Special Targets

There are 4 reserved target names used for special document redirection operations:

_blank

The browser always loads the target document in a newly opened, unnamed window.

_self

The value of this target is the default target for all
tags that do not specify a target, which causes the target document to be loaded and displayed in the same frame or window as the source document. . This target is redundant and unnecessary unless used in conjunction with the target attribute in the tag of the document title.

_parent

This target causes the document to be loaded into the parent window or frameset containing the frame referenced by the hyperlink. If this reference is in a window or in a top-level frame, it is equivalent to target _self.

_top

This goal causes the document to load into the window containing the hyperlink. Using the _top goal will clear all contained frames and load the document into the entire browser window.

Tip: All 4 values ​​of these targets start with an underscore. Any other window or object that begins with an underscore will be ignored by the browser, so do not use an underscore as the first character of any frame name or id defined in the document.

Syntax

<a target="value">

Attribute value

ValueDescription_blankOpen the linked document in a new window. _selfDefault. Open the linked document in the same frame. _parentOpen the linked document in the parent frameset. _topOpen the linked document in the entire window. Open the linked document in the specified frame.
framename


For more HTML target attribute related articles, please pay attention to the PHP Chinese website!