This question already has an answer here:
这个问题在这里已有答案:
- jquery selector unable to find visible last-child 3 answers
jquery选择器无法找到可见的last-child 3答案
<table>
<tr class="here" id="t1" number="1" ><td>1</td></tr>
<tr class="here" id="t2" number="2" ><td>2</td></tr>
<tr class="here" id="t3" number="3" style="display:none"><td>3</td></tr>
<tr class="here" id="t4" number="4" style="display:none"><td>4</td></tr>
</table>
<span id="check">check</span>
$('#check').click(function(){
check = ???;
alert(check);
})
DEMO: http://jsfiddle.net/vUukc/1/
How can I get the attribute number
from last visible tr
in this example? This is an example - all <tr>
could be visible.
如何从此示例中的最后一个可见tr获取属性编号?这是一个例子 - 所有都可见。
3 个解决方案
#1
18
There are different jQuery Selectors for those purposes.
出于这些目的,有不同的jQuery选择器。
For example:
$('table tr:visible:last').attr('number');
or
$('table tr:visible').last().attr('number');
and so on.
等等。
Full code
$('#check').click(function() {
check = $('table tr:visible:last').attr('number');
alert(check);
});
#2
5
$("table tr:visible:last").attr("number");
See the jQuery Documentation on Selectors for more info on the :visible
and :last
selectors.
有关:visible和:last选择器的更多信息,请参阅选择器上的jQuery文档。
#3
2
Try this
$('#check').click(function(){
alert($('table').find('tr:visible:last').attr("number"));
});
#1
18
There are different jQuery Selectors for those purposes.
出于这些目的,有不同的jQuery选择器。
For example:
$('table tr:visible:last').attr('number');
or
$('table tr:visible').last().attr('number');
and so on.
等等。
Full code
$('#check').click(function() {
check = $('table tr:visible:last').attr('number');
alert(check);
});
#2
5
$("table tr:visible:last").attr("number");
See the jQuery Documentation on Selectors for more info on the :visible
and :last
selectors.
有关:visible和:last选择器的更多信息,请参阅选择器上的jQuery文档。
#3
2
Try this
$('#check').click(function(){
alert($('table').find('tr:visible:last').attr("number"));
});