Michael Moncur
Teach Yourself
DHTML in
24 Hours
800 East 96th St., Indianapolis, Indiana, 46240 USA
Sams Teach Yourself DHTML in 24 Hours Copyright © 2002 by Sams Publishing
ACQUISITIONS EDITOR Scott D. Meyers
DEVELOPMENT EDITOR Damon Jordan
All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions. Nor is any liability assumed for damages resulting from the use of the information contained herein.
MANAGING EDITOR
International Standard Book Number: 0-672-32302-8
TECHNICAL EDITOR
Library of Congress Catalog Card Number: 2001096687
02
01
4
Sandy Henselmeier Ken Johnson
PROOFREADER Plan-It Publishing Jason Byars Amy Patton
First Printing: December 2001 03
INDEXERS
TEAM COORDINATOR
Printed in the United States of America
04
Charlotte Clapp
INTERIOR DESIGNER 3
2
1
Trademarks All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Sams Publishing cannot attest to the accuracy of this information. Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark.
Warning and Disclaimer Every effort has been made to make this book as complete and as accurate as possible, but no warranty or fitness is implied. The information provided is on an “as is” basis. The author and the publisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information contained in this book.
Gary Adair
COVER DESIGNER Aren Howell
Contents at a Glance Introduction
PART I Getting Started Hour 1
Understanding DHTML
1
3 5
2
Reviewing HTML
17
3
Understanding JavaScript
33
4
Creating a Simple DHTML Example
49
PART II Learning DHTML Basics Hour 5
59
Understanding the DOM
61
6
Creating Positionable Elements (Layers)
73
7
Working with DOM Properties and Methods
87
8
Responding to Events
PART III Working with Style Sheets Hour 9
101
113
Introducing Style Sheets
115
10
Using Style Sheet Properties
129
11
Controlling Styles with JavaScript
143
12
Creating Consistent Styles
155
PART IV Dynamic HTML in Action
169
Hour 13
Creating Drop-Down Menus
171
14
Creating a Menu Tree
187
15
Creating DHTML Text Effects
199
16
Using DHTML for Animation
213
PART V Learning Advanced Techniques Hour 17
229
Dealing with Browser Differences
231
18
Using DHTML with Forms
245
19
Using Dynamic Fonts
259
20
Troubleshooting DHTML
271
PART VI Putting It All Together Hour 21
285
Creating a Dynamic Site
287
22
Creating Complex Animations
301
23
Creating a DHTML Game
315
24
DHTML Tips and Tricks
333
PART VII Appendixes Appendix A
345
Other JavaScript and DHTML Resources
347
B
Glossary
353
C
Browser Compatibility Chart
357
D
DOM Quick Reference
361
Index
367
Table of Contents Introduction
PART I Getting Started HOUR 1 Understanding DHTML
1
3 5
What Is Dynamic HTML? ......................................................................................6 Static and Dynamic Pages ..................................................................................6 Dynamic HTML Menus ....................................................................................7 Positioning and Animation ................................................................................8 Layers ................................................................................................................8 Text Effects ........................................................................................................8 The Components of DHTML ..................................................................................9 HTML ................................................................................................................9 CSS (Cascading Style Sheets)............................................................................9 The Document Object Model (DOM)..............................................................10 JavaScript..........................................................................................................10 History of DHTML and the DOM ........................................................................11 DOM Level 0....................................................................................................11 Browser-Specific DOMs ..................................................................................12 DOM Level 1....................................................................................................12 DOM Level 2....................................................................................................12 DHTML Browsers ................................................................................................12 Summary ................................................................................................................13 Q&A ......................................................................................................................13 Quiz........................................................................................................................14 Questions ..........................................................................................................14 Answers ............................................................................................................14 Exercises ................................................................................................................15 HOUR 2 Reviewing HTML
17
Basic HTML Syntax ..............................................................................................18 Tags ..................................................................................................................18 Attributes ..........................................................................................................18 The HTML Standard ........................................................................................19 Defining Parts of a Document ..............................................................................19 The , , and Tags..............................................................19 Paragraphs ........................................................................................................20 Headings ..........................................................................................................21 Ordered and Unordered Lists ..........................................................................21
vi
Sams Teach Yourself DHTML in 24 Hours
Images ..............................................................................................................22 Links and Anchors............................................................................................22 Divisions and Spans ........................................................................................22 Styling Text ............................................................................................................23 Bold and Italics ................................................................................................23 Fonts ................................................................................................................24 Indenting ..........................................................................................................24 Using Tables for Layout ..................................................................................24 Creating Forms ......................................................................................................25 The