如何像这样包装所有元素

发布于 2024-10-08 06:42:54 字数 387 浏览 0 评论 0原文

<div id="editor">
  text
  <div id="wrap"> <!-- here -->
    <img src="images/1.jpg"/>
  </div>
  text</p>
  <div id="wrap"> <!-- and here -->
    <img src="images/3.jpg"/>
    <img src="images/4.jpg"/>
    <img src="images/5.jpg"/>
    <img src="images/6.jpg"/>
  </div>
</div>
<div id="editor">
  text
  <div id="wrap"> <!-- here -->
    <img src="images/1.jpg"/>
  </div>
  text</p>
  <div id="wrap"> <!-- and here -->
    <img src="images/3.jpg"/>
    <img src="images/4.jpg"/>
    <img src="images/5.jpg"/>
    <img src="images/6.jpg"/>
  </div>
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅黛梨妆こ 2024-10-15 06:42:54

我想您想要包装 元素的分组。

如果是这样,请执行以下操作:

示例: http://jsfiddle.net/patrick_dw /RXFVM/1/

$('#editor > img:not(img + img)').each(function() {
    $(this).nextUntil(':not(img)').andSelf().wrapAll('<div class="wrap"></div>');
});
  • 'img:not(img + img)':获取前面没有图像的图像
  • .each():迭代
  • .nextUntil(':not(img)'):获取所有下一个元素,只要它们是图像
  • .andSelf():包含原始图像
  • .wrapAll('

    '):包装组

请注意,我将 id="wrap" 更改为 class ="wrap" 因为您不能在页面上重复使用 ID。


编辑:更新了示例链接以使用未损坏的图像。

I imagine you want to wrap groupings of <img> elements.

If so, do this:

Example: http://jsfiddle.net/patrick_dw/RXFVM/1/

$('#editor > img:not(img + img)').each(function() {
    $(this).nextUntil(':not(img)').andSelf().wrapAll('<div class="wrap"></div>');
});
  • 'img:not(img + img)': get images that are not preceded by an image
  • .each(): iterate over them
  • .nextUntil(':not(img)'): get all next elements, as long as they are images
  • .andSelf(): include the original image
  • .wrapAll('<div class="wrap"></div>'): wrap the group

Note that I changed id="wrap" to class="wrap" since you can't reuse IDs on a page.


EDIT: Updated example link to use non-broken images.

美羊羊 2024-10-15 06:42:54

据我了解,你想用 jquery 包装图像?

好吧,它可能看起来像这样

$('img').wrap('<div id="wrap" />');

,但实际上,如果您想要更好的答案,您必须更精确

as i understand it you want to wrap images with jquery?

well it could look like this

$('img').wrap('<div id="wrap" />');

but really you have to be more precise if you want a better answer

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文