(3)选择元素——(5)为项目列表加样式(Styling list-item levels)
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;}
假设我们想要顶部元素,而且只想要顶部元素水平排列。我们可以先在样式表中定一个horizontal类:
.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.
horizontal类让元素浮动到他后续元素的左侧,去掉了他的项目图标,在他的各个方向添加了10像素的边距。
Rather than attaching the horizontalclass directly in our HTML, we'll add it dynamically to the top-level list items only—Comedies, Tragedies, and Histories—to demonstrate jQuery's use of selectors, as follows:
$(document).ready(function() {$('#selected-plays > li').addClass('horizontal');});
我们动态的在顶部元素(仅仅Comedies,Tragedies,Histories)添加了horizontal类,而不是直接在html标签上添加,展示了jquery作为选择器的使用,如下:$(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.
正如在第一章中讨论的那样,我么通过调用$(document).ready()开始我们的jquery代码,他会在DOM加载结束后执行传递给他的代码,而不是之前。
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)."
第二行使用子代选择器仅仅为顶部元素添加horizontal类。他的作用是,$()内部的选择器方法正在说:"找到每一个有着selected-play ID的元素的子元素列表项目(li)"。
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:
在这个类现在被应用后,定义在样式表中的这个类的规则产生了作用。现在我们的嵌套列表看起来像下面的截图一样:
Styling all of the other items—those that are not in the top level—can be done in a number of ways. As we have already applied the horizontalclass to the top-level items, one way to select all sub-level items is to use a negation pseudo-classto 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');
});
为所有的其他元素加上样式——所有的不在顶部的元素——可以使用多种方法实现。正如我们已经为顶级元素加上了top-level元素,一个选择子级元素的方法就是使用反向选择类去确定所有的没有添加horizontal的列表项目。注意下面添加的第三行代码:
$(document).ready(function() {
$('#selected-plays > li').addClass('horizontal');
$('#selected-plays li:not(.horizontal)').addClass('sub-level');
});
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))
这一次,我们选择每一个列表元素li:
是selected-plays ID的后代元素(#delected-plays)
没有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:
当我们添加sub-level类到这些元素的时候,他们就有了定义在样式表中的人灰色背景,现在嵌套列表看起来像像下面的截屏一样。
(3)选择元素——(5)为项目列表加样式(Styling list-item levels)的更多相关文章
- (3)选择元素——(9)为交替的列加样式(Styling alternate rows)
Two very useful custom selectors in the jQuery library are :oddand :even. Let's take a look at how w ...
- Jquery 系列(2) 选择元素
Jquery基础学习 jQuery利用css选择符的能力,能够在DOM中快捷而轻松地获取元素. 主要内容如下: 介绍DOM树 如何通过CSS选择符在页中查找元素 扩展jQuery标准的CSS选择符 选 ...
- JQuery基础教程:选择元素(上)
jQuery最强大的特性之一就是它能够简化在DOM中选择元素的任务,DOM中的对象网络与家谱有几分类似,当我们提到网络中元素之间的关系时,会使用类似描述家庭关系的术语,比如父元素.子元素,等等.通过一 ...
- jQuery选择器对应的DOM API ——选择元素
英文原文:http://blog.garstasio.com/you-dont-need-jquery/selectors/愚人码头注: 原作者的写这文章的意图是让我们抛弃jQuery,You Don ...
- 有一个无效 SelectedValue,因为它不在项目列表中
“Drp_XX”有一个无效 SelectedValue,因为它不在项目列表中 出现以上异常的原因肯定是将DrowDownList控件的SelectedValue属性赋值为一个列表中不存在的值.那么我们 ...
- 15个C++项目列表
实验楼上有很多C++的实战项目,从简单到进阶,学习每个项目都可以掌握相应的知识点. 如果你还是C++新手的话,那么这个C++的项目列表你可以拿去练手实战开发,毕竟学编程动手实践是少不了的! 如果你不知 ...
- 抛弃jQuery:DOM API之选择元素
原文链接:http://blog.garstasio.com/you-dont-need-jquery/selectors/ 我的Blog:http://cabbit.me/you-dont-need ...
- D3.js 其他选择元素方法
在上一节中,已经讲解了 select 和 selectAll,以及选择集的概念.本节具体讲解这两个函数的用法. 假设在 body 中有三个段落元素: <p>Apple</p> ...
- JQuery基础教程:选择元素(下)
DOM遍历方法 利用前面介绍的jQuery选择符取得一组元素,就像是我们在DOM树中纵横遍历再经过筛选得到的结果一样.如果只有这一种取得元素的方式,那我们选择的余地从某个角度讲也是很有限的.很多情 ...
随机推荐
- DB2查询当前时间与指定时间的时间差(相隔的秒数)
DB2查询当前时间与指定时间的时间差(相隔的秒数). 例子:“拍品表 auct_item”中有个“结束时间 end_date”的字段,求结束时间与当前时间的间隔秒数. select (DAYS(a. ...
- 在Ubuntu上下载、编译和安装Android最新内核源代码(Linux Kernel)
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6564592 在前一篇文章提到,从源代码树下载下 ...
- let关键字
作用: 与var类似, 用于声明一个变量特点: 只在块作用域内有效 不能重复声明 不会预处理, 不存在提升应用: 循环遍历加监听 //应用实例 <body> <button>测 ...
- eclipse 连接手机的 核心解决办法
重启adb的方法 根本不是最本质的方法 最本质的问题 ,一句话概括 : 没安装好驱动呗! 下面是转载的 android开发一般用到的开发工具就是eclipe,而安卓手机则用来调试程序.一般新手在建立 ...
- chart.js 示例
一个简单的例子. 1.html代码 <div id="pie" style="width: 250px;float:left"> <h3> ...
- php的一些小笔记-文件函数(1)
---恢复内容开始--- 与文件操作相关的函数有一部分可以和linux命令比较,但是我觉得可能还是linux上使用的比较频繁 如:chown,chmod,chgrp,rename,touch,link ...
- 在ubuntu 12.04 中配置java环境(安装jdk, tomcat, maven, eclipse)
1. 安装jdk 1.7 在ubuntu( /usr/lib/jvm/java-7-openjdk-amd64 )中默认有安装jdk 1.7 如果没有 可下载 : http://www.oracle. ...
- js获取IP地址的方法小结
s代码获取IP地址的三种方法,在js中取得客户端的IP地址. 原文地址:http://www.jbxue.com/article/11338.html 1,js取得IP地址的方法一 <scrip ...
- python中 and 和 or 运算的核心思想 ——— 短路逻辑
python中 and 和 or 运算的核心思想 --- 短路逻辑 1. 包含一个逻辑运算符 首先从基本的概念着手,python中哪些对象会被当成 False 呢?而哪些又是 True 呢? 在Pyt ...
- 利用Azure高级存储搭建高性能Linux服务器(2)
我们首先来测试随机写的IOPS,可以看到随机写的IOPS可以达到,顺序写的IOPS可以达到: $ sudo fio -filename=/data/testfile -direct=1 -iodept ...