[英]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.