czwartek, grudnia 23, 2004

Wykorzystanie ID w HTML-u

Linking to IDs

You probably know that you can link to a specific place in an HTML document by marking that place with a named anchor (e.g. ) and then referring to the name in the link URL (e.g. ). What you may not know is that you can also link to any element that has an ID assigned to it.

The id attribute is supported by every single HTML tag, so you can assign an ID to any part of the page. IDs are most commonly used to single out HTML elements to control the way they look and behave using Cascading Style Sheets (CSS) and JavaScript, respectively.

But assigning an ID to an element also lets you link directly to it. For example, this paragraph has an ID of "intro":

id="intro">This is an
introductory paragraph.

To link to this paragraph from within the same document, simply point to the ID:

#intro">Go to the

introduction

To link to it from another document, also include the document's file name, or full URL as you normally would:

href="example.html#intro">Introduction

Internet Explorer for Windows actually takes this convenience one step further. If you use this method to link to a form field, the browser will give that field focus when the user clicks on the link!


id="email" />


Don't forget to fill in your
email address!

Unfortunately, Internet Explorer for Windows is the only browser to do this, but other browsers will still scroll to the field as with other HTML elements.

Brak komentarzy: