【发布时间】:2014-09-30 05:13:16
【问题描述】:
我的目标是从 WordPress 帖子中删除图像和 iframe(它们位于 .para div 中)并将它们移动到帖子上方的 <ul>,就在本地包装器 (.wrapper940) 内。
下面的 jQuery 可以工作,但会将所有帖子中的所有图像和 iframe 移动到第一个帖子上方的单个新 <ul>。而不是这个,我试图将每个帖子的图像和 iframe 移动到每个帖子的包装内的新 <ul>。
这是 jQuery:
jQuery("document").ready (function($){
var matchesEl = $(".para img, .para iframe");
if ( matchesEl.length > 0) {
var newUl = $("<ul></ul>");
newUl.prependTo(matchesEl.parents(".wrapper940"));
matchesEl.each(function() {
var newLi = $("<li></li>").append($(this)).appendTo(newUl);
});
}
});
html 是:
<div class="news-item-wrap">
<div class="date">the date</div>
<div class="wrapper940">
<div class="title">the title</div>
<div class="para">
<p>The main content of the post.</p>
<p>Which could be several paragraphs</p>
<p>And include iframes...</p>
<p><iframe src="//www.youtube.com/embed/uGMbZNTym-g" width="560" height="315" frameborder="0" allowfullscreen="allowfullscreen">...</iframe>
</p>
<p>Followed by more text... and maybe some images....</p>
<p><a href="http://www.joujouka.org/wp-content/uploads/2014/05/festival-interculture.jpg"><img class="alignnone size-medium wp-image-404" alt="festival intercultural" src="http://www.joujouka.org/wp-content/uploads/2014/05/festival-interculture-213x300.jpg" width="213" height="300"/></a>
</p>
</div>
</div>
<div class="news-item-wrap">
<div class="date">the date</div>
<div class="wrapper940">
<div class="title">the title</div>
<div class="para">
<p>A second post would follow like this.</p>
<p>Which could also be several paragraphs</p>
<p>And include iframes...</p>
<p><iframe src="//www.youtube.com/embed/uGMbZNTym-g" width="560" height="315" frameborder="0" allowfullscreen="allowfullscreen">...</iframe>
</p>
<p>Followed by more text... and maybe some images....</p>
<p><a href="http://www.joujouka.org/wp-content/uploads/2014/05/festival-interculture.jpg"><img class="alignnone size-medium wp-image-404" alt="festival intercultural" src="http://www.joujouka.org/wp-content/uploads/2014/05/festival-interculture-213x300.jpg" width="213" height="300"/></a>
</p>
</div>
</div>
这将持续到尽可能多的帖子。因此,我需要能够将每个 POST 的图像和 iframe 移动到包装每个 POST 的 .wrapper940 内。 (即在每个帖子的标题上方。)
我认为使用.parents() 会将所有帖子中的所有图像和iframe 发送到第一个.wrapper940; .closest() 似乎应该可以工作,但没有,可能是因为它打破了循环?
非常感谢任何帮助!
【问题讨论】:
标签: jquery wordpress html-lists posts