4. 使用DOM的遍曆方法
在第2節中我們使用屬性選取器實現了表徵圖添加的工作,
那麼我們能否使用遍曆的方式來進行此類操作呢?答案是能。
在選取器之後增加一個遍曆方法如:“filter”
$('tr').filter(':odd').addClass('alt'); 在遍曆中我們可以進行一些處理,例如,可以在chapter02-1.html的連結處增加一個表徵圖
css代碼
a.external {background: #fff url(images/external.png) no-repeat 100% 2px;padding-right: 16px;} js代碼
$('a').filter(function() { return this.hostname && this.hostname != location.hostname; }).addClass('external');
實現效果如下:
那麼除了“filter”之外有沒有其他的遍曆方法了呢?<喎?http://www.bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+ICAgICAgICDPwsPmtcShsG5leHShsbrNobBuZXh0QWxsobG+zcrHxuTW0Nau0ruhozwvcD4KPHA+ICAgICAgICDU2rXaM73a1tDO0sPH0tG+rcq508NqUXVlcnm1xNfUtqjS5bXE0aHU8cb3yrXP1sHLxbzK/cHQtcS118mryejWw6OsPC9wPgo8cD4gICAgICAgIMTHw7TE3LK7xNyw0cbk1tC1xMSzuPbK/b7dJiMyNjY4NDu438HBxNijvzwvcD4KPHA+ICAgICAgICCxyMjnyc+92tbQztLDx8jDobBIZW5yeaGxuN/BwcHLo6zE3LfxyMPL+9auuvO1xNK7uPa1pdSquN/BwaGjPC9wPgo8cD4gICAgICAgIM7Sw8e94brPyvTQ1NGh1PHG98C0zeqzybTLstnX96GjPC9wPgo8cD4gICAgICAgIGpztPrC6zwvcD4KPHA+PHByZSBjbGFzcz0="brush:java;">$('td:contains(Henry)').next().addClass('highlight'); 實現效果
Shakespeare's Plays
| As You Like It |
Comedy |
|
| All's Well that Ends Well |
Comedy |
1601 |
| Hamlet |
Tragedy |
1604 |
| Macbeth |
Tragedy |
1606 |
| Romeo and Juliet |
Tragedy |
1595 |
| Henry IV, Part I |
History |
1596 |
| Henry V |
History |
1599 |
Shakespeare's Sonnets
| The Fair Youth |
1-126 |
| The Dark Lady |
127-152 |
| The Rival Poet |
78-86 |
用nextAll可以讓本列之後的所有單元格高亮。
$('td:contains(Henry)').nextAll().addClass('highlight');
實現效果
Shakespeare's Plays
| As You Like It |
Comedy |
|
| All's Well that Ends Well |
Comedy |
1601 |
| Hamlet |
Tragedy |
1604 |
| Macbeth |
Tragedy |
1606 |
| Romeo and Juliet |
Tragedy |
1595 |
| Henry IV, Part I |
History |
1596 |
| Henry V |
History |
1599 |
Shakespeare's Sonnets
| The Fair Youth |
1-126 |
| The Dark Lady |
127-152 |
| The Rival Poet |
78-86 |
整列高亮的時候,我們需要點DOM樹形知識了!
TH和TD的父節點是TR
那麼我們就要用到兩個方法:parent(), children()
首先,我們定位TD;然後,尋找她的父節點(TR);最後,再尋找此列(TD+);
js代碼
$('td:contains(Henry)').parent().children().addClass('highlight'); 實現效果
Shakespeare's Plays
| As You Like It |
Comedy |
|
| All's Well that Ends Well |
Comedy |
1601 |
| Hamlet |
Tragedy |
1604 |
| Macbeth |
Tragedy |
1606 |
| Romeo and Juliet |
Tragedy |
1595 |
| Henry IV, Part I |
History |
1596 |
| Henry V |
History |
1599 |
Shakespeare's Sonnets
| The Fair Youth |
1-126 |
| The Dark Lady |
127-152 |
| The Rival Poet |
78-86 |