(3)選擇元素——(5)為項目列表加樣式(Styling list-item levels)

來源:互聯網
上載者:User

Let's suppose that we want the top-level items, and only the top-level items, to be arranged horizontally. We can start by defining a horizontalclass in the stylesheet:

.horizontal {float: left;list-style: none;margin: 10px;}


The horizontal class floats the element to the left 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.


$(document).ready(function() {$('#selected-plays > li').addClass('horizontal');});


As discussed in Chapter 1, we begin the jQuery code by calling $(document).ready(), which runs the function passed to it as soon as the DOM has loaded but not before.


The second line uses the child combinator(>) to add the horizontalclass to all 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. Now our nested list looks similar to the following screenshot:


$('#selected-plays > li').addClass('horizontal');
$('#selected-plays li:not(.horizontal)').addClass('sub-level');
});



•  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-levelclass to these items, they receive the shaded background defined in the stylesheet. Now the nested list looks similar to the following screenshot:


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.