使用Jquery插入HTML(限制次数)

时间:2016-03-26 14:36:49

标签: javascript jquery html

我正在尝试使用JQuery在每个Feed项之后插入一段HTML代码,但无论我尝试它还是行不通。我在我正在尝试编辑的页面下面显示了HTML代码。我想在每个提要时间之后插入一些HTML(每次用class="wp_rss_retriever_container">完成时)

<div class="wp_rss_retriever">
    <ul class="wp_rss_retriever_list">
        <li class="wp_rss_retriever_item">
            <div class="wp_rss_retriever_item_wrapper">
                <a class="wp_rss_retriever_title" target="_blank" href="http://footballnewsdaily.net/uncategorized/man-united-ready-to-smash-transfer-record-to-sign-star-striker/" title="Man United ready to smash transfer record to sign star striker">
                    Man United ready to smash transfer record to sign star striker
                </a>

                <div class="wp_rss_retriever_container">

                    <div class="wp_rss_retriever_metadata">
                        <span class="wp_rss_retriever_date">
                            Published: March 25, 2016 - 12:29 pm
                        </span>
                    </div>
                </div>
            </div>
        </li>

        <li class="wp_rss_retriever_item">
            <div class="wp_rss_retriever_item_wrapper">
                <a class="wp_rss_retriever_title" target="_blank" href="http://footballnewsdaily.net/manchester-united/fenerbahce-plan-bid-for-manchester-united-ace-mata/" title="Fenerbahce plan bid for Manchester United ace Mata">
                    Fenerbahce plan bid for Manchester United ace Mata
                </a>

                <div class="wp_rss_retriever_container">

                    <div class="wp_rss_retriever_metadata">
                        <span class="wp_rss_retriever_date">
                            Published: March 25, 2016 - 12:15 pm
                        </span>
                    </div>
                </div>
            </div>
        </li>

        <li class="wp_rss_retriever_item">
            <div class="wp_rss_retriever_item_wrapper">
                <a class="wp_rss_retriever_title" target="_blank" href="http://footballnewsdaily.net/manchester-united/united-arsenal-target-morata-premier-league-would-suit-me/" title="Manchester United, Arsenal target Morata: Premier League would suit me">
                    Manchester United, Arsenal target Morata: Premier League would suit me
                </a>
                <div class="wp_rss_retriever_container">

                    <div class="wp_rss_retriever_metadata">
                        <span class="wp_rss_retriever_date">
                            Published: March 25, 2016 - 11:55 am
                        </span>
                    </div>
                </div>
            </div>
        </li>

        <li class="wp_rss_retriever_item">
            <div class="wp_rss_retriever_item_wrapper">
                <a class="wp_rss_retriever_title" target="_blank" href="http://footballnewsdaily.net/manchester-united/manchester-united-rival-arsenal-liverpool-for-juventus-striker-morata/"

我试图使用的代码是

$( "<p>Test</p>" ).insertAfter( ".wp_rss_retriever_container" );

1 个答案:

答案 0 :(得分:0)

您可以使用每个迭代所有.wp_rss_retriever_container类:

$('.wp_rss_retriever_container').each(function(i, obj) {
    $(this).append("<p>Test</p>"); //or .html()
});

如果您希望它有限的次数&#39;如果&#39; i&#39;你可以调理和打破等于某个值:

if(i == 5){ break; }  // only 4 'test' will be added

希望对你有所帮助!