Day007
jQuery 中有哪些方法可以遍历节点?
1.children()方法:$('div').children()---遍历查找div元素的所有子元素节点
1
2
3
4
5
6
7
8
9
10
|
<p>Hello</p> <div> <span>Hello Again</span>
<p class= "box" >您好!</p>
</div> <p>And Again</p> <script type= "text/javascript" >
$( 'div' ).children(); //<span>Hello Again</span><p class="box">您好!</p>
$( 'div' ).children( '.box' ) //<p class="box">您好!</p>
</script> |
2.next()方法:$('div').next() --- 查找div元素后相邻的同级元素但非所有同级元素
[相关方法]
(1)nextAll()方法:$('div').nextAll() ---- 查找div之后的所有同级元素
(2)nextUntil()方法:$('div').nextUntil('p')----查找div之后直到p元素的所有同级元素
1
2
3
4
5
6
7
8
9
|
<p>Hello</p> <p class= "box" >Hello Again</p>
<div> <span>And Again</span>
</div> <script type= "text/javascript" >
$( 'p' ).next(); //<p>Hello Again</p><div><span>And Again</span></div>
$( 'p' ).next( '.box' ); //<p class="box">Hello Again</p>
</script> |
3.prev()方法:$('div').prev() ---- 查找div之前相邻的同级元素
[相关方法有]
(1)prevAll()方法:$('div').prevAll() ---- 查找div之前的所有同级元素
(2)prevUntil()方法:$('div').prevUntil('p') --- 查找div之前直到p元素的所有元素
1
2
3
4
5
6
7
8
|
<p>Hello</p> <div> <span>Hello Again</span>
</div> <p>And Again</p> <script type= "text/javascript" >
$( 'p' ).prev(); //<div><span>Hello Again</span></div>
</script> |
4.siblings()方法:$('div').siblings()---- 查找div前后所有的同级元素
5.find()方法:$('div').find('span') ---- 查找div元素内子元素并且是span元素
6.eq()方法:$('div').eq(1) --- 查找第二个div元素(索引下标从0开始)
7.first()方法:$('li').first() --- 获取第一个li元素
8.last()方法:$('li').last() --- 获取最后一个li元素
9.filter()方法:$('div').filter('.box') --- 获取类名为box的div元素
10.is()方法:$('.box').is('div') ---- 判断.box是否是div元素
11.map()方法:$('div').map(callback) --- 将每个div执行callback函数
例:遍历input元素获取其value值以“,”分隔添加到p元素内后面
1
2
3
4
5
6
7
8
9
10
11
|
<p><b>Values: </b></p> <form> <input type= "text" name= "name" value= "John" />
<input type= "text" name= "password" value= "password" />
</form> <script type= "text/javascript" >
$( "p" ).append( $( "input" ).map( function (){
return $( this ).val();
</script> |
12.hasClass()方法:$('div').hasClass(‘box') ---- 查找含有类名为box的div
13.has()方法:$('div').has('span') ---- 查找含包有span元素的div元素
14.not()方法:$('div').not('span') ---- 查找不包含有span元素的div元素
15.slice()方法:$('p').slice(0,2) ---- 查找第1个p元素到第3个p元素
16.offsetParent()方法:$('p').offsetParent() --- 查找p元素的第一个被定位的祖先元素
17.parent()方法:$('p').parent() ---- 返回包含p元素的唯一父节点的元素集合
18.parents()方法:$('p').parent() ---- 返回包含p元素的所有祖先节点(不包括根节点)
19.parentUntil()方法:$('p').parentUntil('#box') ---- 查找p元素的祖先级元素直到#box为止
20.contents()方法:$('div').contents() --- 返回div元素内的所有子节点(包括文本节点)
21.end()方法:$('div').find('span').end() ---- 将语句的主体变回前一次状态即:查找到span元素之后焦点返回到div元素
1
2
3
4
5
6
7
8
|
<div> <span>Hello</span>,
how are you?
</div> <script type= "text/javascript" >
$( 'div' ).find( 'span' ).addClass( 'test' ).end().attr( 'title' , 'title1' );
//span添加class=test;div添加title=title1
</script> |
考虑这样一个表:【联系人】(姓名,性别,电话)
如果在实际场景中,一个联系人有家庭电话和公司电话,那么这种表结构设计就没有达到 1NF。要符合 1NF 我们只需把列(电话)拆分,即:【联系人】(姓名,性别,家庭电话,公司电话)。1NF 很好辨别,但是 2NF 和 3NF 就容易搞混淆。
◆ 第二范式(2NF):首先是 1NF,另外包含两部分内容,一是表必须有一个主键;二是没有包含在主键中的列必须完全依赖于主键,而不能只依赖于主键的一部分。
考虑一个订单明细表:【OrderDetail】(OrderID,ProductID,UnitPrice,Discount,Quantity,ProductName)。
因为我们知道在一个订单中可以订购多种产品,所以单单一个 OrderID 是不足以成为主键的,主键应该是(OrderID,ProductID)。显而易见 Discount(折扣),Quantity(数量)完全依赖(取决)于主键(OderID,ProductID),而 UnitPrice,ProductName 只依赖于 ProductID。所以 OrderDetail 表不符合 2NF。不符合 2NF 的设计容易产生冗余数据。
可以把【OrderDetail】表拆分为【OrderDetail】(OrderID,ProductID,Discount,Quantity)和【Product】(ProductID,UnitPrice,ProductName)来消除原订单表中UnitPrice,ProductName多次重复的情况。
◆ 第三范式(3NF):首先是 2NF,另外非主键列必须直接依赖于主键,不能存在传递依赖。即不能存在:非主键列 A 依赖于非主键列 B,非主键列 B 依赖于主键的情况。
考虑一个订单表【Order】(OrderID,OrderDate,CustomerID,CustomerName,CustomerAddr,CustomerCity)主键是(OrderID)。
其中 OrderDate,CustomerID,CustomerName,CustomerAddr,CustomerCity 等非主键列都完全依赖于主键(OrderID),所以符合 2NF。不过问题是 CustomerName,CustomerAddr,CustomerCity 直接依赖的是 CustomerID(非主键列),而不是直接依赖于主键,它是通过传递才依赖于主键,所以不符合 3NF。
通过拆分【Order】为【Order】(OrderID,OrderDate,CustomerID)和【Customer】(CustomerID,CustomerName,CustomerAddr,CustomerCity)从而达到 3NF。