使用jquery在DOM中重新放置标记

时间:2013-03-10 11:16:36

标签: jquery css dom joomla joomla-k2

我一直在尝试在现有的CMS制作的html中重新定位img标记

<div class="catItemView groupLeading _inspirations">
 <!-- (2) and place it here -->
 <div class="catItemHeader">
     <h3 class="catItemTitle">
         <a href="some link here">ways of seeing</a>
      </h3>
 </div>
 <div class="catItemBody">
     <div class="catItemIntroText">
         <p>
            <a href="youtube link here">
               <img alt="" src="image_link.jpg" /><!-- (1) would like to grab this-->
               John Berger - ways of seeing
            </a>
         </p>
     </div>
    </div>
 <div class="catItemLinks">
 </div>
 </div>    

我试过了:

 $('div.catItemIntroText img').prepend('div.catItemHeader');

但它只会混乱所有其他'帧',并用所有图像填充'帧'(就像这一帧)。 提前谢谢!

1 个答案:

答案 0 :(得分:0)

目前你正在将div添加到图像中。这会将图像添加到div:

$('.catItemView').prepend($('.catItemIntroText img'));

如果您在页面上多次使用此结构,则需要执行此操作以获得每次出现的所需结果:

$('.catItemIntroText img').each(function() {
    $(this).parents('.catItemView').prepend($(this));
});