- Learning jQuery(Fourth Edition)
- Jonathan Chaffer Karl Swedberg
- 658字
- 2021-08-13 17:18:46
CSS selectors
The jQuery library supports nearly all the selectors included in CSS specifications 1 through 3, as outlined on the World Wide Web Consortium's site: http://www.w3.org/Style/CSS/specs. This support allows developers to enhance their websites without worrying about which browsers might not understand more advanced selectors, as long as the browsers have JavaScript enabled.
Tip
Progressive Enhancement
Responsible jQuery developers should always apply the concepts of progressive enhancement and graceful degradation to their code, ensuring that a page will render as accurately, even if not as beautifully, with JavaScript disabled as it does with JavaScript turned on. We will continue to explore these concepts throughout the book. More information on progressive enhancement can be found at http://en.wikipedia.org/wiki/Progressive_enhancement.
To begin learning how jQuery works with CSS selectors, we'll use a structure that appears on many websites, often for navigation – the nested unordered list:
<ul id="selected-plays"> <li>Comedies <ul> <li><a href="/asyoulikeit/">As You Like It</a></li> <li>All's Well That Ends Well</li> <li>A Midsummer Night's Dream</li> <li>Twelfth Night</li> </ul> </li> <li>Tragedies <ul> <li><a href="hamlet.pdf">Hamlet</a></li> <li>Macbeth</li> <li>Romeo and Juliet</li> </ul> </li> <li>Histories <ul> <li>Henry IV (<a href="mailto:henryiv@king.co.uk">email</a>) <ul> <li>Part I</li> <li>Part II</li> </ul> <li><a > Henry V</a></li> <li>Richard II</li> </ul> </li> </ul>
Tip
Downloadable code examples
As with many of the HTML, CSS, and JavaScript examples in this book, the previous markup is merely a fragment of the complete document. To experiment with the examples, we can download them from the Packt Publishing website at http://www.packtpub.com/support. In addition, the examples can be viewed in an interactive browser at http://book.learningjquery.com/.
Notice that the first <ul>
has an ID of selecting-plays
, but none of the <li>
tags have a class associated with them. Without any styles applied, the list looks like this:

The nested list appears as we would expect it to—a set of bulleted items arranged vertically and indented according to their level.
Styling list-item levels
Let's suppose that we want the top-level items, and only the top-level items—Comedies, Tragedies, and Histories—to be arranged horizontally. We can start by defining a horizontal
class in the stylesheet:
.horizontal { float: left; list-style: none; margin: 10px; }
The horizontal
class floats the element to the left-hand side of the one following it, removes the bullet from it if it's a list item, and adds a 10-pixel margin on all sides of it.
Rather than attaching the horizontal
class directly in our HTML, we'll add it dynamically to the top-level list items only, to demonstrate jQuery's use of selectors:
$(document).ready(function() { $('#selected-plays > li').addClass('horizontal'); });
Listing 2.1
As discussed in Chapter 1, Getting Started, we begin the jQuery code by calling $(document).ready()
, which runs the function passed to it once the DOM has been loaded, but not before.
The second line uses the child combinator (>
) to add the horizontal
class to all the top-level items only. In effect, the selector inside the $()
function is saying, "Find each list item (li
) that is a child (>
) of the element with an ID of selected-plays (#selected-plays
)"
With the class now applied, the rules defined for that class in the stylesheet take effect, which in this case means that the list items are arranged horizontally rather than vertically. Now our nested list looks like this:

Styling all the other items—those that are not in the top level—can be done in a number of ways. Since we have already applied the horizontal
class to the top-level items, one way to select all sub-level items is to use a negation pseudo-class to identify all list items that do not have a class of horizontal
. Note the addition of the third line of code:
$(document).ready(function() {
$('#selected-plays > li').addClass('horizontal');
$('#selected-plays li:not(.horizontal)').addClass('sub-level');li:not(.horizontal)').addClass('sub-level');
});
Listing 2.2
This time we are selecting every list item (<li>
) that:
- Is a descendant of the element with an ID of
selected-plays
(#selected-plays
) - Does not have a class of
horizontal
(:not(.horizontal)
)
When we add the sub-level
class to these items, they receive the shaded background defined in the stylesheet:
.sub-level { background: #ccc; }
Now the nested list looks like this:

- Learn Blockchain Programming with JavaScript
- Learning Cython Programming(Second Edition)
- Getting started with Google Guava
- 重學Java設計模式
- The HTML and CSS Workshop
- Learning Hunk
- Visual FoxPro程序設計
- Corona SDK Mobile Game Development:Beginner's Guide(Second Edition)
- Django Design Patterns and Best Practices
- iOS開發項目化入門教程
- Arduino Electronics Blueprints
- INSTANT PLC Programming with RSLogix 5000
- 測試工程師Python開發實戰
- Three.js Essentials
- Python深度學習與項目實戰