[英]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>
元素都具有BBListOddRowStyle
或BBListEvenRowStyle
類, BBListOddRowStyle
可以執行以下操作:
$('table[id*="dgRegistrantList"]').find('tr[class*=RowStyle]:last')
.addClass('EventRegLastAttendee')
如果沒有,您可以使用.children()
兩次以確保您定位到正確的對象:
$('table[id*="dgRegistrantList"]').children('tbody')
.children('tr:last').addClass('EventRegLastAttendee')
使用此代碼定位最后一行:
$('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.