简体   繁体   English

jQuery用类找到li直到

[英]jQuery find li with class until

This might be tricky. 这可能很棘手。

Php will generate: PHP将生成:

<li>element</li>
<li>element</li>
<li class="alt">element</li>
<li class="alt">element</li>
<li class="alt">element</li>
<li>element</li>
<li>element</li>
<li class="alt">element</li>
<li class="alt">element</li>
<li class="alt">element</li>
<li>element</li>

What I need is a way to find the li elements with the .alt class until the li elements start. 我需要的是一种使用.alt类查找li元素的方法,直到li元素开始。 So... 所以...

<li>element</li>
<li>element</li> // if I click on this one, it will select
<li class="alt">element</li> // this one
<li class="alt">element</li> // and this one
<li class="alt">element</li> // and this one
<li>element</li> // stops selecting here
<li>element</li>
<li class="alt">element</li> // doe not this one
<li class="alt">element</li> // or this one
<li class="alt">element</li> // or any other .alt
<li>element</li>

I tried doing this with .find(), but I can't find a way to divide the .alt elements into groups and isolate the first one. 我尝试使用.find()进行此操作,但是找不到将.alt元素划分为组并隔离第一个元素的方法。

Any braniacs there with a good idea? 有什么好主意吗?

If you're using jquery 1.4 you can use the new nextUntil method. 如果您使用的是jQuery 1.4,则可以使用新的nextUntil方法。

http://api.jquery.com/nextUntil/ http://api.jquery.com/nextUntil/

edit - example: 编辑-示例:

$("li").click(function() {

   $(this).nextUntil("li[class='']").each(function() {           
    console.log($(this));           
   });

});

try (couple of edits but think I have it) 尝试(几个编辑,但认为我有)

$('li:contains(element)').click().find('li:contains(element):not[alt]:first').prevAll('li.alt'); $( '礼:包含(元素)')点击()找到( '礼:包含(元素):没有[ALT]:第一')prevAll( 'li.alt');。

您不能为要检索的lis添加额外的类吗?

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

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