In jQuery, trying to get next-item-not-having-a-sp

2019-06-17 01:13发布

问题:

I'm trying to get at the first not(.hidden) div after the .focus div. In this example the focus is X2, and I should get back X5, skipping X3 and X4 because they are hidden.

<div id='CONTAINER'>
    <div id='X1'>eks1</div>
    <div id='X2' class='focus'>eks2</div>
    <div id='X3' class='hidden'>eks3</div>
    <div id='X4' class='hidden'>eks4</div>
    <div id='X5'>eks5</div>
    <div id='X6'>eks6</div>
</div>

This seems like it should be a pretty simple jquery question, but I'm fairly new to this stuff.

回答1:

The below statement should solve your problem. $("#CONTAINER").find("div.focus").nextAll("div").not(".hidden").first();



回答2:

Use the nextAll method: http://api.jquery.com/next/

$('div.focus').nextAll('div:not(.hidden)').filter(':first');

This will find the next div after .focus that is not .hidden



回答3:

try this!

$("#CONTAINER").find("div.focus").next('div[class!="hidden"]')


回答4:

$nextNotHidden = $('.focus').nextUntil(':not(.hidden)').last().next()


回答5:

Try this:

var stop = true;
$('#CONTAINER> div.focus +div').each(function(){
    if(!$(this).hasClass('hidden') && stop) code
    else stop = false;
});