(3)選擇元素——(12)為特定的方塊加樣式(Styling specific cells)

來源:互聯網
上載者:User

Earlier we added a highlightclass to all cells containing the text Henry. To style the cell next to each cell containing Henry, we can begin with the selector that we have already written, and simply call the .next()method on the result, as follows:

$(document).ready(function() {$('td:contains(Henry)').next().addClass('highlight');});

The tables should now look similar to the following screenshot:


之前,我們為所有包含文本Henry的方格添加了一個highlight類。為了給每一個包含Henry下一個方格加樣式,我們可以以我們已經寫下的選取器開始,然後僅僅在結果中調用一下.next()方法,如下:
$(document).ready(function() { $('td:contains(Henry)').next().addClass('highlight');});
現在,這個表看起來像下面的一樣:

The .next()method selects only the very next sibling element. To highlight all of the cells following the one containing Henry, we could use the .nextAll()method instead:

$(document).ready(function() {$('td:contains(Henry)').nextAll().addClass('highlight');});
As the cells containing Henryare in the first column of the table, this code causes the rest of the cells in these rows to be highlighted, as shown in the following screenshot:

.next()方法僅僅選擇了恰好挨著的兄弟節點。為了讓包含Henry元素後面的所有的方塊高亮,我們應該使用.nextAll()方法:
$(document).ready(function() { $('td:contains(Henry)').nextAll().addClass('highlight');});

$(document).ready(function() {$('td:contains(Henry)').nextAll().andSelf() .addClass('highlight');});

With this modification in place, all of the cells in the row get the styling offered by the highlightclass, as follows:

$(document).ready(function() {    $('td:contains(Henry)').parent().children().addClass('highlight');});
Here, rather than traversing across to sibling elements, we travel up one level in the DOM to the <tr>with .parent()and then select all of the row's cells with .children().






相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.