[英]Select second to last element
我需要選擇倒數第二個輸入可選元素的值:
<tr><td><select class="x">...</select></td></tr>
<tr><td><select class="x">...</select></td></tr>
<tr><td><select class="x">...</select></td></tr>
選擇輸入標簽位於tr標簽內。
如果我使用$("select.x").last()
,jQuery選擇最后一個元素。 我需要選擇倒數第二名。
你可以使用.prev()
$("select.x").last().prev();
下面的所有內容都可以解決問題(選擇第二個最后一個元素):
$("select.x").eq(select.length - 1)
$("select.x:nth-last-of-type(2)")
$("select.x:nth-last-child(2)")
$("select.x").last().prev()
你需要使用jquery的“nth-last-child(2)”,這將選擇第二個最后一個元素。
你可以在這里查看:
使用.prev()或nth-last-child()的解決方案不起作用。
<tr><td><a>...</a><select class="x"></select><a>...</a><td><tr>
<tr><td><a>...</a><select class="x"></select><a>...</a><td><tr>
<tr><td><a>...</a><select class="x"></select><a>...</a><td><tr>
<tr><td><a>...</a><select class="x"></select><a>...</a><td><tr>
問題是last()。prev()函數返回對象<a>
,我首先選擇的是對象。
nth-last-of-type(2)選擇器改為返回一個空對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.