简体   繁体   English

当元素集不总是在同一个父元素内时,如何找到下一个匹配的元素

[英]How do you find the next matched element(s), when the set of elements are not always inside the same parent

I am modifying a calendar plugin. 我正在修改日历插件。 The plugin adds a class of today to todays date. 该插件在今天的日期添加了一类。 I would like to add the class of "foo" to "today" and the next 2 days after this. 我想将“foo”类添加到“今天”以及接下来的2天。

Rather than each month being 1 unordered-list, the plugin generates a new unordered-list for each row. 该插件不是每个月都是1个无序列表,而是为每一行生成一个新的无序列表。

What would be the best way to find the appropriate list items when they cross into different parent elements? 当它们进入不同的父元素时,找到适当的列表项的最佳方法是什么?

Case 1 情况1

<ul>
    <li class="day"></li>
    <li class="day"></li>
    <li class="day today foo"></li>
    <li class="day foo"></li>
    <li class="day foo"></li>
    <li class="day"></li>
    <li class="day"></li>
</ul>

Case 2 案例2

<ul>
    <li class="day"></li>
    <li class="day"></li>
    <li class="day"></li>
    <li class="day"></li>
    <li class="day"></li>
    <li class="day"></li>
    <li class="day today foo"></li>
</ul>
<ul>
    <li class="day foo"></li>
    <li class="day foo"></li>
    <li class="day"></li>
    <li class="day"></li>
    <li class="day"></li>
    <li class="day"></li>
    <li class="day"></li>
</ul>

I have been looking at .find, .nextAll, .nextUntil but not sure how to do it. 我一直在看.find,.nextAll,.nextUntil,但不知道怎么做。 Or should I create an array with all the days and then split it at today or something? 或者我应该创建一个包含所有日子的数组,然后在今天或其他地方拆分它?

You can find all the days, then find out where in that jQuery set the "today" one is via index(element) , and then use slice to get just that and the two following: 你可以找到所有的日子,然后找出jQuery设置中的“今天”通过index(element) ,然后使用slice来获取它和以下两个:

var days = $(".day")
var todayIndex = days.index($(".today"));
days.slice(todayIndex, todayIndex + 3).addClass("foo");

Live Example (first case) : 实例 (第一种情况)

 var days = $(".day") var todayIndex = days.index($(".today")); days.slice(todayIndex, todayIndex + 3).addClass("foo"); 
 .today { border: 1px solid #ddd; } .foo { color: blue; } 
 <ul> <li class="day">xxxx</li> <li class="day">xxxx</li> <li class="day today">xxxx</li> <li class="day">xxxx</li> <li class="day">xxxx</li> <li class="day">xxxx</li> <li class="day">xxxx</li> </ul> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 

Live Example (second case) : 实例 (第二种情况)

 var days = $(".day") var todayIndex = days.index($(".today")); days.slice(todayIndex, todayIndex + 3).addClass("foo"); 
 .today { border: 1px solid #ddd; } .foo { color: blue; } 
 <ul> <li class="day">xxxx</li> <li class="day">xxxx</li> <li class="day">xxxx</li> <li class="day">xxxx</li> <li class="day">xxxx</li> <li class="day">xxxx</li> <li class="day today">xxxx</li> </ul> <ul> <li class="day">xxxx</li> <li class="day">xxxx</li> <li class="day">xxxx</li> <li class="day">xxxx</li> <li class="day">xxxx</li> <li class="day">xxxx</li> <li class="day">xxxx</li> </ul> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 

暂无
暂无

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

相关问题 如何找到 Protractor 中的下一个元素? - How do you find the next element in Protractor? 使用JavaScript,如何使用getElementByClassName获取具有位于另一个元素内的特定className的元素? - Using JavaScript, how do you get elements with a specific className that's inside another element by using getElementByClassName? 如何隐藏基于一系列父元素的HTML元素 - How do you hide an HTML element based on a series of parent elements 如何找到与给定元素相同的 div 中存在的元素的类? - How to find the classes of the elements present inside the same div as a given element? 在一组匹配元素上将父元素添加到父级,并使用Parent Div href - Javascript - Add Child Element to Parent on a Set of Matched Elements, and use Parent Div href - Javascript 在模糊/焦点事件上,如果下一个焦点位于同一个父元素中,如何才能使模糊不触发? - On blur/focus events, how can you get blur not to fire if the next focus is in the same parent element? 在一组匹配元素中的一个元素上触发单击事件,而不是在匹配元素上触发 - Fire click event on one element in a set of matched elements and not on matched elements 如何在元素循环内获取元素的className? - How do I get element's className inside loop of elements? 当Jquery找不到元素时,Jquery最接近()缓慢我的应用程序,如何更快地执行相同的查询? - Jquery's closest() slow's my app down when it doesn't find an element, how do I do the same query but faster? 如何在不使用jQuery的情况下基于子元素的文本选择父元素? - How do you select a parent element based on the text of it's child element WITHOUT jQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM