我已经在网上搜了几个小时试图找到一个对我有用的解决方案,但我似乎找不到一个。
我需要将图片([alt="post-banner"]
)移到#blog-banner-container中。问题是,具有该alt的所有图像都被移动到第一个div中。我假设我必须使用.closest()
,但我不知道如何使用它。
的jQuery
$('[alt="post-banner"]').appendTo('#blog-banner-container');
HTML
<div class="blog-entry">
<div id="blog-banner-container"></div>
<div class="blog-entry-title">Title</div>
<div class="blog-entry-body">
<img src="#" alt="post-banner" />
</div>
</div>
<div class="blog-entry">
<div id="blog-banner-container"></div>
<div class="blog-entry-title">Title</div>
<div class="blog-entry-body">
<img src="#" alt="post-banner" />
</div>
</div>
<div class="blog-entry">
<div id="blog-banner-container"></div>
<div class="blog-entry-title">Title</div>
<div class="blog-entry-body">
<img src="#" alt="post-banner" />
</div>
</div>
答案 0 :(得分:0)
您可以选择父级,然后选择具有您要查找的ID的级别的兄弟。
<强> HTML 强>
<div class="blog-entry">
<div id="blog-banner-container"></div>
<div class="blog-entry-title">Title</div>
<div class="blog-entry-body">
<img src="#" alt="post-banner" />
</div>
</div>
<div class="blog-entry">
<div id="blog-banner-container"></div>
<div class="blog-entry-title">Title</div>
<div class="blog-entry-body">
<img src="#" alt="post-banner" />
</div>
</div>
<div class="blog-entry">
<div id="blog-banner-container"></div>
<div class="blog-entry-title">Title</div>
<div class="blog-entry-body">
<img src="#" alt="post-banner" />
</div>
</div>
<强>的Javascript 强>
$('[alt="post-banner"]').each(function() {
$(this).parents().siblings('#blog-banner-container').append(this);
});