用jquery用div包裹多个选定元素

克里斯蒂安·摩恩

我想用jquery从A标签到br标签中选择并用div包裹它。

我是开发 chrome 扩展程序的第三方开发人员,我使用以下数据:

<div id="indiv" style="font-size: 12px">
    <a href="/?side=profiles&id=60438"><span style="color: #2498E0">Esmeralda Angelseye</span></a> (21:12): content here
    <br>

    <a href="/?side=profiles&id=39818"><span style="color: #800909">Ernest Ferdinand Solis</span></a> (21:09): content here
    <br>

    <a href="/?side=profiles&id=60438"><span style="color: #2498E0">Esmeralda Angelseye</span></a> (21:12): content here
    <br>

    <a href="/?side=profiles&id=39818"><span style="color: #800909">Ernest Ferdinand Solis</span></a> (21:09): content here
    <br>
</div>

如您所见,每个“帖子”都以 a in 和 br 结尾。

我不知道我应该如何接近,我已经测试了一些疯狂的东西,比如:

$("<div>").insertBefore("#indiv a");
$("</div>").insertAfter($("#indiv a").next().next());

Jquery 会自动在 div 之后附加 /div ..

我希望它最终看起来如何:

<div id="indiv" style="font-size: 12px">
    <div>
      <a href="/?side=profiles&id=60438"><span style="color: #2498E0">Esmeralda Angelseye</span></a> (21:12): content here
      <br>
    </div>

    <div>
      <a href="/?side=profiles&id=60438"><span style="color: #2498E0">Esmeralda Angelseye</span></a> (21:12): content here
      <br>
    </div>

    <div>
      <a href="/?side=profiles&id=60438"><span style="color: #2498E0">Esmeralda Angelseye</span></a> (21:12): content here
      <br>
    </div>

    <div>
      <a href="/?side=profiles&id=60438"><span style="color: #2498E0">Esmeralda Angelseye</span></a> (21:12): content here
      <br>
    </div>
</div>

有希望吗?

内曼贾德

就从我的头顶。添加两个元素类内部。像这样使用 Jquery:

$('#indiv div a').addClass('inner')

对 br 做同样的事情。

或者手动添加。

<a class="inner" href="/?side=profiles&id=60438"><span style="color: #2498E0">Esmeralda Angelseye</span></a> (21:12): content here
          <br class="inner">

并将它们都包裹起来。

$('.inner').wrapAll('<div></div>');

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章