簡體   English   中英

定位最后一個PARENT表行(tr),而不是CHILD表行(tr)

[英]Targeting the last PARENT table-row (tr), not the CHILD table-row (tr)

我正在嘗試定位其中具有子表行元素的表中的最后一個父表行。 我已經嘗試使用下面的jQuery來定位:last偽對象,但是,正如預期的那樣,它的目標是目標父表中的絕對last table-row元素。

$('table[id*="dgRegistrantList"]').find('tr:last').addClass('EventRegLastAttendee')

我已經把一個jsFiddle與我試圖用jQuery作為目標的HTML塊放在一起,我希望它有用! http://jsfiddle.net/jodriscoll/LZA7e/

綠色=目標;紅色=忽略 Green table-row是我想要定位的那一行,然而,Red中突出顯示的那一行是接收該類的明顯行。

該系統可以根據在“步驟”之前的用戶選擇生成表行的變體。 有關我正在使用的軟件的完整示例,請訪問: http : //secure.massgeneral.org/event-form (我正在使用步驟2)。

請注意,我正在使用的HTML是由我作為客戶的CMS軟件生成的,無法更改。 因此,此jQuery練習的目的。

如果所有父<tr>元素都具有BBListOddRowStyleBBListEvenRowStyle類, BBListOddRowStyle可以執行以下操作:

$('table[id*="dgRegistrantList"]').find('tr[class*=RowStyle]:last')
    .addClass('EventRegLastAttendee')

DEMO

如果沒有,您可以使用.children()兩次以確保您定位到正確的對象:

$('table[id*="dgRegistrantList"]').children('tbody')
    .children('tr:last').addClass('EventRegLastAttendee')

DEMO

使用此代碼定位最后一行:

$('table[id*="dgRegistrantList"]').find('tr[class^=BBList][class$=RowStyle]:last').addClass('EventRegLastAttendee')

說明

tr //it will look for tr
[class^=BBList] //which class starts with BBList
[class$=RowStyle] //and ends with RowStyle (so we're leaving Odd and Even inside and not recognized)
:last //the last of those element, if you remove it you select all of them

您要執行.children()嗎?

$('table[id*="dgRegistrantList"]').children('tr:last').addClass('EventRegLastAttendee');

.children()只下降到一個dom級別,而.find()會盡可能地下降。

不要使用find 它將查看任何深度,並且可能與意外的子表匹配。 也許這對您的示例有用,但是您不想養成不良習慣。 另外, find比定向方法更昂貴。

您需要更有針對性的方法:

var targetTd = $('table[id*="dgRegistrantList"]').children('tbody').children('tr:last').find('table:first').children('tbody').children('td:last');

使用此代碼將父tr替換為最后一行

$('table[id*="dgRegistrantList"]').find('tr[class^=BBList]:last').addClass('EventRegLastAttendee');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM