我有以下输出:
<ul id="blogs">
<li>
<a href="/blogs/news/6875583-hurricane-sandy">
<div class="video-container">...</div>
</a>
</li>
<a href="/blogs/news/6875583-hurricane-sandy">
<strong id="title">Hurricane Sandy</strong>
<p id="newsp">loremi ispum lor ismi smodi</p>
</a>
<a id="readp" class="rmore" href="/blogs/news/6875583-hurricane-sandy">Read More</a>
<li>
<a href="/blogs/news/6572743-floyd"></a>
<div class="video-container">...</div>
</a>
</li>
<a href="/blogs/news/6572743-floyd"></a>
<strong id="title">Hurricane Floyd</strong>
<p id="newsp">loremi ispum lor ismi smodi</p>
</a>
<a id="readp" class="rmore" href="/blogs/news/6572743-floyd">Read More</a>
<li>
<a href="/blogs/news/6557987-fiji">
<img li="" ...<="" src="//mydomain/A21E1A04F48C9B7F42333F72EB5A57D9.jpg?932">
<strong id="title">Fiji.</strong>
<p id="newsp">loremi ispum lor ismi smodi</p>
</a>
<a id="readp" class="rmore" href="/blogs/news/6557987-fiji">Read More</a>
</li>
</ul>
我想用 jQuery 编辑上面的输出,如下所示:
<ul id="blogs">
<li>
<a href="/blogs/news/6875583-hurricane-sandy">
<img src="http://mydomain/video_icons.jpg?1212">
</a>
<strong id="title">Hurricane Sandy</strong>
<p id="newsp">loremi ispum lor ismi smodi</p>
<a id="readp" class="rmore" href="/blogs/news/6875583-hurricane-sandy">Read More</a>
</li>
<li>
<a href="/blogs/news/6572743-floyd"></a>
<img src="http://mydomain/video_icons.jpg?1212">
</a>
<strong id="title">Hurricane Floyd</strong>
<p id="newsp">loremi ispum lor ismi smodi</p>
<a id="readp" class="rmore" href="/blogs/news/6572743-floyd">Read More</a>
</li>
<li>
<a href="/blogs/news/6557987-fiji">
<img src="//mydomain/A21E1A04F48C9B7F42333F72EB5A57D9.jpg?932">
</a>
<strong id="title">Fiji.</strong>
<p id="newsp">loremi ispum lor ismi smodi</p>
<a id="readp" class="rmore" href="/blogs/news/6557987-fiji">Read More</a>
这是我的 jQuery,但我遗漏了一些东西或没有正确编写它,所以我没有得到想要的输出:
jQuery:
$("#blogs li").each(function(i) {
if ($(".video-container").length > 0 && $(this).children('img').length == 0){
var vidindex = $('#readp').attr('href')
$(this).find('a').replaceWith(function(){return '';});
$(".video-container").remove();
$(this).append('<a href=""><img src="{{ 'video_icons.jpg' | asset_url }}"/></a>');
$(this).append($("#title"));
$(this).append($('#newsp'));
$(this).append($('#readp'))
$(this).find('a').attr("href", vidindex)
}
$("#blogs li img").replaceWith(function(){
return '<img src="'+this.src+'"/>';
});
});