有没有一种简便的方式来执行nextUntil操作,以便包括与选择器匹配的元素?我有这样一个代码,如果存在前面的同级元素则正常工作:
$("#content").find("h3:first").prev().nextUntil("ul:last").wrapAll("<div id='collapse'></div>");
有没有一种简便的方式来执行nextUntil操作,以便包括与选择器匹配的元素?我有这样一个代码,如果存在前面的同级元素则正常工作:
$("#content").find("h3:first").prev().nextUntil("ul:last").wrapAll("<div id='collapse'></div>");
删除.prev()
,将.nextUntil
替换为.nextAll()
,并在您的选择器末尾使用.addBack()
,如下所示:
$("#content").find("h3:first").nextAll("ul:last").addBack().wrapAll("<div id='collapse'></div>");
在1.8版本之前应该使用andSelf
而不是addBack
nextUntil
(或prevUntil
)获取length
,然后在nextAll()
上使用这个长度+1的slice()
函数:var inclusiveNextUntil = $(this).nextUntil( '.some-class' ).length;
var inclusiveNextUntil = $(this).nextAll().slice( 0 , inclusiveNextUntil + 1 );
它很笨拙,但它起作用。HTH
就 nextAll 文档而言,Biziclop 提到了添加 + *:(链接)
$obj.nextUntil('.last-item-to-include + *')
var s = $("li.start").nextUntil("li.stop");
s = s.add(s.last().next()).add(s.first().prev());
//and do whatever you need to with s
var elearr = $('selector1').nextUntil('selector2');
var lastele = elearr[elearr.length-1];
elearr.push($(lastele).next());
正如其他人所回答的那样,使用.addSelf()
是使起始元素包含在内的解决方案。
$("#content").find("h3:first").nextUntil("ul:last").addSelf().wrapAll("<div id='collapse'></div>");
为了使结束元素包含在内,使用.nextUntil(".class + *")
方法解决了这个问题。
$("#content").find("h3:first").nextUntil("ul:last + *").addSelf().wrapAll("<div id='collapse'></div>");
我认为正确的答案是
$("#content").find("h3:first").nextUntil("ul:last").addSelf().wrapAll("<div id='collapse'></div>");