wrapAll jQuery问题
我需要将三个div包装成一个,但是这组三个div在代码中重复几次。 也许我的HTML会解释:
我想要实现的是:
这将所有东西都包装成一个div:
$('.one, .two, .three').wrapAll('')
如何将这些组分开包装?
$(function(){ var all = $('.one, .two, .three'); for(i=0; i
看到它在行动: http : //jsbin.com/uqosa/
顺便说一下,
在Firefox上对我不起作用,我不得不用关闭它们。
试试这个:
$('div.one').each(function() { var wrap = $(this).wrap('').parent(); wrap.next('div.two').appendTo(wrap); wrap.next('div.three').appendTo(wrap); });
这将有效:
$(function() { var $ones = $('div.one'); var $twos = $('div.two'); var $threes = $('div.three'); $ones.each(function(idx) { $('') .append($ones.eq(idx)) .append($twos.eq(idx)) .append($threes.eq(idx)) .appendTo('body'); }); });
在我的脑海中这应该工作:
$('.one').each(function() { var wrap = $('').addClass('wrap'), two = $(this).next(), three = $(two).next(); $(this).after(wrap).appendTo(wrap) two.appendTo(wrap), three.appendTo(wrap) });
$.fn.matchUntil = function(expr) { var match = []; this.each(function(){ match = [this]; for ( var i = this.nextSibling; i; i = i.nextSibling ) { if ( i.nodeType != 1 ) { continue; } if ( $(i).filter(expr).length > 0 ) { break; } match.push( i ); } }); return this.pushStack( match ); };
用法:
$(".one").each( function() { $(this).matchUntil(".one").wrapAll(""); })
它只是http://docs.jquery.com/JQuery_1.2_Roadmap#.nextUntil.28.29_.2F_.prevUntil.28.29的修改版本,它似乎不适用于新的jQuery。