包装一系列没有父母的元素?

时间:2018-01-27 21:52:30

标签: javascript jquery html

所以,通常我会使用.wrap()方法并将所选元素包装到父元素中,但我不能。我使用内置于我网站的 RSS Feed工具。此RSS提要工具显示RSS提要项目,但每个提要项目的项目(标题,图像,描述)不具有父项以将其他提要项目与其余项目分开。你最终得到的是连续堆栈而不是元素。这是Feed的一个示例:

<br><br>
<span class="rssFeedTitle j-rss-feed-title">blah</span>
(Fri, 26 Jan 2018) 
<br><br>

<a href="https://www.reddit.com/r/science/comments/7t7td0/if_you_swat_mosquitos_they_may_learn_to_avoid/"><img src="https://a.thumbs.redditmedia.com/eoypOMK7F7ASgmQMh4jtCk9lk0JkV5ZvgthnZI_wJU0.jpg" alt="blah" title="blah"></a>


submitted by   <a href="https://www.reddit.com/user/sleekpaprika69">/u/sleekpaprika69</a>
<a href="http://www.cell.com/current-biology/fulltext/S0960-9822(17)31617-2">[link]</a>   <a href="https://www.reddit.com/r/science/comments/7t7td0/if_you_swat_mosquitos_they_may_learn_to_avoid/">[comments]</a>

<br>
<a href="https://www.reddit.com/r/science/comments/7t7td0/if_you_swat_mosquitos_they_may_learn_to_avoid/" target="_blank">&gt;&gt; Read More</a>
<br><br>

这一段代码,是一篇文章(链接,img,标题,日期,用户)。下一个故事后跟着<br>标记的相同结构。

上面的所有代码都应该包含:

<div id="article">
... the code above ...
</div>

但它不是,我不知道如果除BR标签之外没有其他任何指定,我将如何包装这样的内容。

自己看看:https://wiredintel.jimdo.com/

1 个答案:

答案 0 :(得分:0)

$(".rssFeedTitle").each(
function (index) {
    $(this).nextUntil(".rssFeedTitle").addBack().wrapAll("<div class='mm'></div>");
});


$(function() {

    var test = $('.rssFeed')
        .contents()
        .filter(function() {
            return this.nodeType === 3 && $.trim(this.nodeValue) !== '';
        })
        .wrap('<div class="wrappedtext" />');

});
$(function() {
$('.wrappedtext:contains("subm")').css('display', 'none');
});

http://jsfiddle.net/zachreynolds/rem12umh/2/

相关问题