【问题标题】:jQuery: Wrap elements with nextUntil()jQuery:用 nextUntil() 包装元素
【发布时间】:2014-07-13 10:31:34
【问题描述】:
<h2 id="match" class="contentHeadline">Match only in here</h2>

<p>I don't if or how many dl+table live here.</p>

<dl><dt>Text</dt></dl>
<table class="groupTable"><td>1</td><td>Abc</td></table>

<dl><dt>Text</dt></dl>
<table class="groupTable"><td>2</td><td>Def</td></table>

<dl><dt>Text</dt></dl>
<table class="groupTable"><td>3</td><td>Ghi</td></table>

<p class="foo">Maybe some text lives here. I should not float.</p>

我想将每对 dl + table 包装在一个 div.box(浮动)中,但只能直接在 h2#match 下,直到后面有其他内容(.foo 或标题)。

$("dl").each(function(){
    $(this).next('.groupTable').andSelf()
        .wrapAll('<div class="box"></div>');
});

/* Clear */
$('<div class="clear"></div>').after('.box:last');

nextUntil() 单独包装 dl 和表格。

atm 测试组被错误包装。

测试:http://jsfiddle.net/GQwB5/

想要的结果:http://jsfiddle.net/kppQ9/

【问题讨论】:

  • “测试”组的表格元素没有class = 'groupTable'
  • 嗯,看起来你已经在 desired result 小提琴中工作了.. 那么问题是什么..?顺便说一句,“但仅在 h2#match 的正下方,直到后面的其他内容(.foo 或标题)”是什么意思。 ..??
  • 期望的结果是明确它应该如何看待最后。我用 jquery 将它们包装到 div.box 中。我的意思是我需要在 h2#match 下选择每个 dl+table.groupTable 直到其他非 dl+table 的东西像 p.foo 或一些 h2 一样跟随。

标签: jquery wrapall nextuntil


【解决方案1】:

$('#match').nextUntil('.contentHeadline') 效果很好。

应用:

$('#match').nextUntil('.contentHeadline').filter('dl').each(function(){
    $(this).next('.groupTable').andSelf().wrapAll('<div class="box" />');
});
$('#match').nextUntil('.contentHeadline').filter('.box:last').after('<div class="clear" />');

小提琴:http://jsfiddle.net/GQwB5/1/

我怀疑.nextUntil(".foo, .contentHeadline") 不起作用,因为它需要一个匹配元素,并且至少会返回两个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-21
    • 1970-01-01
    • 1970-01-01
    • 2014-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多