将多个图像包装在
内;在 jQuery 中

发布于 2024-08-26 01:21:23 字数 626 浏览 3 评论 0原文

我需要找到 div 内的所有图像,并用 div 包裹它们。这是我想出的代码,但这不起作用!为什么?

jQuery(function() {
  my_selection = [];
  $('.post').each(function(i) {
    if ($(this).find('img').length > 1) {
      my_selection.push(['.post:eq(' + i + ')']);
    }
  });
  $(my_selection.join(',')).wrapAll('<div class="wrapper"></div>');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

I need to find all the images inside a div, and wrap a div around them. Here's the code I come up with, but that's not working! Why?

jQuery(function() {
  my_selection = [];
  $('.post').each(function(i) {
    if ($(this).find('img').length > 1) {
      my_selection.push(['.post:eq(' + i + ')']);
    }
  });
  $(my_selection.join(',')).wrapAll('<div class="wrapper"></div>');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

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

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

发布评论

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

评论(6

潜移默化 2024-09-02 01:21:23

怎么样:

$('.post img').wrapAll('<div class="wrapper" />');

.post img 将在 .post 容器中获取 IMG 标签的集合,并且 wrapAll 应用 DIV 围绕着他们每个人。

wrapAll 函数的手册页 实际上有一个非常接近您想要的示例。

How about something like:

$('.post img').wrapAll('<div class="wrapper" />');

.post img will get a collection of IMG tags within your .post container, and wrapAll applies the DIV around each of them.

The manual page for the wrapAll function actually has quite a close example to what you want.

怪我鬧 2024-09-02 01:21:23

很难说,因为我看不到你的标记,但类似:

$('.post img').wrapAll('<div class="wrapper" />');

Hard to say because I can't see your markup but something like:

$('.post img').wrapAll('<div class="wrapper" />');
清秋悲枫 2024-09-02 01:21:23

这有效!

$('.post').each(function(){
    var container = $(this);
    $('img', container).wrapAll('<div class="slideshow" />');
});

this works!

$('.post').each(function(){
    var container = $(this);
    $('img', container).wrapAll('<div class="slideshow" />');
});
深居我梦 2024-09-02 01:21:23

试试这个

 $('.post img').each(function() {
      $(this).wrap($('<div/>', { 'class': 'wrapper'}));
    });

这里是我问的类似问题的链接:

创建新的div单击时围绕锚链接

Try this

 $('.post img').each(function() {
      $(this).wrap($('<div/>', { 'class': 'wrapper'}));
    });

Here is a link to the similar question I asked :

Create new div arround anchor link when clicked

心作怪 2024-09-02 01:21:23
$('img').each(function (){
   $(this).wrap('<div class="new" />');
});
$('img').each(function (){
   $(this).wrap('<div class="new" />');
});
猫卆 2024-09-02 01:21:23

将 div 包裹在 .post 内的每个 img 周围:

$('.post img').wrap('<div class="wrapper" />');

将 div 包裹在每个具有 img 的帖子周围:

$('.post:has(img)').wrap('<div class="wrapper" />');

将所有具有 img 的 div 移动到包装器 div 内:

$('<div class="wrapper" />').append($('.post:has(img)'));

Wrap a div around each img inside a .post:

$('.post img').wrap('<div class="wrapper" />');

Wrap a div around each post that has a img:

$('.post:has(img)').wrap('<div class="wrapper" />');

Move all divs that have an img inside a wrapper div:

$('<div class="wrapper" />').append($('.post:has(img)'));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文