简体   繁体   English

jquery prev-next 可见选择器

[英]jquery prev-next visible selector

I want to select prev or next visible element, jumping over hidden ones.我想prev上一个或next可见元素,跳过隐藏的元素。

So click on the first title shoud write c in console, but it doesn't work.所以点击第一个标题应该在控制台中写c ,但它不起作用。

 $('.atitle').on('click', function(){ console.log($(this).next(':visible').text()); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class='atitle'>a</div> <div class='atitle' hidden>b</div> <div class='atitle'>c</div> <div class='atitle'>d</div>

You can try with nextAll(':visible:first') and prevAll(':visible:first') :您可以尝试使用nextAll(':visible:first')prevAll(':visible:first')

 $('.atitle').on('click', function(){ console.clear(); console.log($(this).nextAll(':visible:first').text() + ':Next'); console.log($(this).prevAll(':visible:first').text() + ':Prevoius'); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class='atitle'>a</div> <div class='atitle' hidden>b</div> <div class='atitle'>c</div> <div class='atitle'>d</div>

 $('.atitle').on('click', function(){ if($(this)[0] == $('.atitle:last')[0]){ // for some reason $(this).is(':last') not working console.log("EOL"); return null; } var atitle = findNext($(this).next('.atitle')); if(atitle) console.log(atitle.text()); }); function findNext(elem){ if($(this)[0] == $('.atitle:last')[0]){ console.log("EOL"); return null; } if(elem.is(":visible")) return elem; else return findNext(elem.next('.atitle')); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class='atitle'>a</div> <div class='atitle' style="display:none">b</div> <div class='atitle'>c</div> <div class='atitle'>d</div>

Well, as you stated that you want to select the next or the prev visible element, I'm assuming that you want only one of them and it should be the closest one between both.好吧,正如您所说,您想要prev next或上一个可见元素,我假设您只想要其中一个,它应该是两者之间最接近的一个。 To achieve this, one solution is to select the next visible one, the previous visible one and compare between them which one is the closest.为了实现这一点,一种解决方案是 select 下一个可见的,前一个可见的,并在它们之间比较哪一个最接近。 So, taking into account the next layout:因此,考虑到下一个布局:

<div class='atitle'>a</div>
<div class='atitle' hidden>b</div>
<div class='atitle'>c</div>
<div class='atitle'>d</div>
<div class='atitle' hidden>e</div>
<div class='atitle'>f</div>
<div class='atitle' hidden>g</div>
<div class='atitle'>h</div>

Clicking on each element should return:单击每个元素应返回:

┌─────────────┬───────────────┐
│ Clicking on │ Should return │
├─────────────┼───────────────┤
│ a           │ c             │
│ c           │ d             │
│ d           │ c             │
│ f           │ d or h*       │
│ h           │ f             │
└─────────────┴───────────────┘

* As d and h are at the same distance from f , if the later is clicked, you should decide which one of the first ones you are going to select if you want only one, in this example, I'm selecting both. * 由于dhf的距离相同,如果单击后者,则如果您只想要一个,您应该决定您要去 select 的第一个,在这个例子中,我选择两者。

 $('.atitle').on('click', function() { var $this = $(this); // Select the closest prev and next visible elements var $prev = $this.prevAll('.atitle:visible:eq(0)'); var $next = $this.nextAll('.atitle:visible:eq(0)'); // Check the indexes of each one var prevDiff = $prev.index() < 0? NaN: Math.abs($this.index() - $prev.index()); var nextDiff = $next.index() < 0? NaN: Math.abs($this.index() - $next.index()); // Decide which one should be selected if (isNaN(prevDiff) || nextDiff < prevDiff) { console.log( $next.text() ); } else if (isNaN(nextDiff) || prevDiff < nextDiff) { console.log( $prev.text() ); } else if (nextDiff === prevDiff) { console.log($prev.text(), $next.text()); } else { console.log('no element'); } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class='atitle'>a</div> <div class='atitle' hidden>b</div> <div class='atitle'>c</div> <div class='atitle'>d</div> <div class='atitle' hidden>e</div> <div class='atitle'>f</div> <div class='atitle' hidden>g</div> <div class='atitle'>h</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM