jQuery Cookbook
jQuery Cookbook
jQuery Community Experts
Beijing • Cambridge • Farnham • Köln • Sebastopol • Taipei • Tokyo
jQuery Cookbook by jQuery Community Experts Copyright © 2010 Cody Lindley. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or
[email protected]
Editor: Simon St.Laurent Production Editor: Sarah Schneider Copyeditor: Kim Wimpsett Proofreader: Andrea Fox Production Services: Molly Sharp
Indexer: Fred Brown Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrator: Robert Romano
Printing History: November 2009:
First Edition.
O’Reilly and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. jQuery Cookbook, the image of an ermine, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein.
TM
This book uses RepKover, a durable and flexible lay-flat binding. ISBN: 978-0-596-15977-1 [S] 1257774409
Table of Contents
Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi Contributors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii 1. jQuery Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.1 Including the jQuery Library Code in an HTML Page 1.2 Executing jQuery/JavaScript Coded After the DOM Has Loaded but Before Complete Page Load 1.3 Selecting DOM Elements Using Selectors and the jQuery Function 1.4 Selecting DOM Elements Within a Specified Context 1.5 Filtering a Wrapper Set of DOM Elements 1.6 Finding Descendant Elements Within the Currently Selected Wrapper Set 1.7 Returning to the Prior Selection Before a Destructive Change 1.8 Including the Previous Selection with the Current Selection 1.9 Traversing the DOM Based on Your Current Context to Acquire a New Set of DOM Elements 1.10 Creating, Operating on, and Inserting DOM Elements 1.11 Removing DOM Elements 1.12 Replacing DOM Elements 1.13 Cloning DOM Elements 1.14 Getting, Setting, and Removing DOM Element Attributes 1.15 Getting and Setting HTML Content 1.16 Getting and Setting Text Content 1.17 Using the $ Alias Without Creating Global Conflicts
9 10 13 15 16 18 19 20 21 23 24 26 27 29 30 31 32
2. Selecting Elements with jQuery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 2.1 Selecting Child Elements Only 2.2 Selecting Specific Siblings
36 37
v
2.3 2.4 2.5 2.6 2.7 2.8 2.9 2.10 2.11 2.12
Selecting Elements by Index Order Selecting Elements That Are Currently Animating