我正在尝试选择display:block
在隐藏的父元素中具有属性的每个最后一个元素。
html:
<ul style="display:none;">
<li>1</li>
<li>2</li>
<li>3</li><!-- my goal is find last visible child on every hidden ul -->
<li style="display:none;">4</li>
</ul>
jQuery:
$('ul').each(function() {
visibleCountOnEachUl =+ 0;
$(this).children('li').each(function(index) {
if ( $(this).css('display') === 'block' ) {
visibleCountOnEachUl += 1;
$(this).addClass('theseAreVisible');
//this part works;
//i can select elems elems which got display:block
//but can't select the last elem on each ul
//if ( index === (visibleCount-1) ) {
//$(this).addClass('last-visible');
//}
}
});
});
$('ul').find('li:visible:last').addClass('last-visible');
//This won't effect on child elements while their parent is hidden.
我发现了这个类似的问题,但解决方案适用于可见的父级。