官术网_书友最值得收藏!

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:

主站蜘蛛池模板: 呼伦贝尔市| 靖边县| 疏附县| 应用必备| 新晃| 景德镇市| 新蔡县| 黔南| 武义县| 库尔勒市| 青海省| 扎兰屯市| 沙洋县| 当涂县| 乌兰县| 巨野县| 甘肃省| 桐梓县| 儋州市| 汝阳县| 栾城县| 绥德县| 三河市| 安多县| 井陉县| 蒙城县| 上高县| 吕梁市| 西城区| 扶绥县| 西和县| 迁西县| 宜昌市| 汉寿县| 沙坪坝区| 商城县| 建德市| 礼泉县| 新兴县| 克什克腾旗| 乳源|