如何追加到最近的div(按类)?
我正在尝试做一些 jQuery,它将在文本中查找图像,将图像包装在 div 中,然后将其移动到另一个 div,但我似乎无法让它工作。
这是我迄今为止有效的代码:
$(".newsIntro").find("img").wrap("<div class=\"newsImage\" />");
但是,当我尝试移动它时,页面上的所有图像要么移动到一个 div 内(而不是移动到其父 div (“.newsItem”),要么什么也不会发生。
$(".newsImage").appendTo( $(this).closest(".newsItem") );
上面的代码没有执行任何操作:
$(".newsImage").appendTo(".newsItem");
上面的代码将它们全部移动到第一个 .newsItem
div
下面是 HTML 片段:
<div class="newsItem">
<div class="newsHeader">
<h2><a href="/news-information/67-latest-news-03.html">Latest News 03</a></h2>
</div>
<div class="newsIntro"><img src="/images/stories/opals-are-lucky/img_abopal_lucky.jpg" border="0" align="left" />Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Fusce dictum sagittis sapien. Aliquam erat volutpat. Nulla facilisi. Ut purus neque, condimentum nec,
auctor eget, semper ut, enim. Vestibulum sem tellus, vestibulum a, vehicula ut, feugiat id, libero. Pellentesque enim justo, condimentum sed, dictum at, viverra eget, odio. Aliquam feugiat metus id lacus. Cum sociis natoque penatibus et magnis dis
parturient montes, nascetur ridiculus mus. Nam iaculis iaculis quam. Donec eu dui.</div>
<div class="clear"></div>
</div>
<div class="newsItem">
<div class="newsHeader">
<h2><a href="/news-information/68-latest-news-03.html">Latest News 03</a></h2>
</div>
<div class="newsIntro"><img src="/images/stories/about-us/img_showroom.jpg" border="0" align="right" />Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Fusce dictum sagittis sapien. Aliquam erat volutpat. Nulla facilisi. Ut purus neque, condimentum nec, auctor eget,
semper ut, enim. Vestibulum sem tellus, vestibulum a, vehicula ut, feugiat id, libero. Pellentesque enim justo, condimentum sed, dictum at, viverra eget, odio. Aliquam feugiat metus id lacus. Cum sociis natoque penatibus et magnis dis parturient montes,
nascetur ridiculus mus. Nam iaculis iaculis quam. Donec eu dui.</div>
<div class="clear"></div>
</div>
I'm trying to do some jQuery which will find images inside text, wrap the image inside a div and then move it to another div, yet I can't seem to get it working.
Here's the code that I have so far which works:
$(".newsIntro").find("img").wrap("<div class=\"newsImage\" />");
But, when I try to move it, either all images on the page move inside the one div (instead of moving to their parent div (".newsItem"), or nothing happens.
$(".newsImage").appendTo( $(this).closest(".newsItem") );
The above doesn't do anything:
$(".newsImage").appendTo(".newsItem");
The one above that moves them all into the first .newsItem
div
.
Here's a snippet of the HTML:
<div class="newsItem">
<div class="newsHeader">
<h2><a href="/news-information/67-latest-news-03.html">Latest News 03</a></h2>
</div>
<div class="newsIntro"><img src="/images/stories/opals-are-lucky/img_abopal_lucky.jpg" border="0" align="left" />Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Fusce dictum sagittis sapien. Aliquam erat volutpat. Nulla facilisi. Ut purus neque, condimentum nec,
auctor eget, semper ut, enim. Vestibulum sem tellus, vestibulum a, vehicula ut, feugiat id, libero. Pellentesque enim justo, condimentum sed, dictum at, viverra eget, odio. Aliquam feugiat metus id lacus. Cum sociis natoque penatibus et magnis dis
parturient montes, nascetur ridiculus mus. Nam iaculis iaculis quam. Donec eu dui.</div>
<div class="clear"></div>
</div>
<div class="newsItem">
<div class="newsHeader">
<h2><a href="/news-information/68-latest-news-03.html">Latest News 03</a></h2>
</div>
<div class="newsIntro"><img src="/images/stories/about-us/img_showroom.jpg" border="0" align="right" />Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Fusce dictum sagittis sapien. Aliquam erat volutpat. Nulla facilisi. Ut purus neque, condimentum nec, auctor eget,
semper ut, enim. Vestibulum sem tellus, vestibulum a, vehicula ut, feugiat id, libero. Pellentesque enim justo, condimentum sed, dictum at, viverra eget, odio. Aliquam feugiat metus id lacus. Cum sociis natoque penatibus et magnis dis parturient montes,
nascetur ridiculus mus. Nam iaculis iaculis quam. Donec eu dui.</div>
<div class="clear"></div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用
.each()
循环,所以this
指的是每个元素,如下所示:因为
.appendTo()
无论如何都会翻转到.append()
,走这条路效率更高。You need to loop through using
.each()
sothis
refers to each element as you go, like this:Since
.appendTo()
gets flipped around to.append()
anyway, it's more efficient to go this route.