我需要jQuery .nextUntil()
的替代方案。我目前正在使用jQuery 1.3.1
并且更新它是不可能的:(
我有这个HTML:
<h4>...</h4>
<p>...</p>
<p>...</p>
<p>...</p>
<h4>...</h4>
<p>...</p>
<p>...</p>
我有这个jQuery代码:
$('h4').click(function(){
$(this).nextUntil('h4').toggle();
});
但.nextUntil()
中添加了1.4.0
因此,您是否知道如何在1.3.1
中执行此操作?
答案 0 :(得分:5)
您可以一起使用nextUntil(),nextAll()和slice()来模仿index()的行为:
var $nextAll = $(this).nextAll();
$nextAll.slice(0, $nextAll.index("h4")).toggle();
答案 1 :(得分:4)
$('h4').click(function() {
$(this).nextAll().each(function() {
if ($(this).is('h4')) { return false; }
$(this).toggle();
})
});
未经测试。经@ingo测试:)
答案 2 :(得分:1)
未经过测试,但是这样:
function nextUntil($start, until)
{
var matches = [];
for (var e = $start.next(); e.length !== 0 && !e.is(until); e = e.next())
{
matches.push(e);
}
return $(matches);
}
或使用nextAll()
:
function nextUntil_v2($start, until)
{
var matches = [];
$start.nextAll().each(function ()
{
if ($(this).is(until)) return false;
matches.push(this);
});
return $(matches);
}
答案 3 :(得分:0)
我这样做了
$('h4').click(function(){
$n = $(this).next();
while($n.is('h4') == false) {
$n.toggle();
$n = $n.next();
}
});
答案 4 :(得分:0)
如果有人仍然需要使用jQuery 1.3:jQuery Untils plugin已添加到jQuery 1.4.0
:
jQuery Untils提供了三种非常简单但非常有用的方法:nextUntil,prevUntil和parentsUntil。这些方法基于他们的nextAll,prevAll和父对应方,除了它们允许您在达到某个选择器时停止。元素以“遍历顺序”返回。
...
在Internet Explorer 6-8,Firefox 2-3.7,Safari 3-4,Chrome 4-5,Opera 9.6-10.1中使用jQuery 1.3.2进行测试。