javascript - 过滤新内容以仅显示特定的 div

标签 javascript jquery

为了在页面中加载更多内容,我使用 jQuery.get() 函数检索外部页面链接。

我可以检索所有页面,但是,当我尝试过滤结果以仅获取“.container_journal_post”div 时,我无法使其工作。 欢迎您的帮助。

这是我当前的 HTML 代码:

<div id="related_projects" class="gridalicious">
    <article><div class="container_journal_post"> post 1 </div></article>
    <article><div class="container_journal_post"> post 2 </div></article>
</div>

这是我的外部页面 HTML 代码:

<div id="related_projects" class="gridalicious">
    <article><div class="container_journal_post"> post 34 </div></article>
    <article><div class="container_journal_post"> post 35 </div></article>
</div>

这是我的 JavaScript 函数:

jQuery(document).ready(function($) {
    var pageNum = parseInt($("#current_page")[0]["innerHTML"]);
    // Get the page number 
    var max = parseInt($("#max_num_pages")[0]["innerHTML"]);
    // Get the maximum number of the blog section.
    var nextLink = $("#pagination .next")[0]["href"];
    // Get The link of the next page of posts.

    // A button where the user is cliking to load more posts
    $('#append').click(function(){
        makeboxes();
    }); 

    makeboxes = function() {
        var boxes = new Array;

        $.get(nextLink, function(data) {
           // console.log(data);
           // Return the "nextLink" HTML DOM
           // Can't cut it correctly 
           // console.log($(data));
           // Return 100 items

           // I would like to get something like this:
                $(data).filter( 'article' ).each(
                function( key, value ) { 
//                  value has to be equal at 
//                  <div class="container_journal_post"> post 34 </div>
                    boxes.push(value);
                }

            );
            // load at the end of the div the new boxes
            jQuery( "#related_projects" ).gridalicious( 'append', boxes );
        });
    }
});

调用“makebox”函数后想要的结果:

<div id="related_projects" class="gridalicious">
    <article><div class="container_journal_post"> post 1 </div></article>
    <article><div class="container_journal_post"> post 2 </div></article>
    <article><div class="container_journal_post"> post 34 </div></article>
    <article><div class="container_journal_post"> post 35 </div></article>
</div>

最佳答案

你可以使用jquery的load函数:

$('#result').load('ajax/test.html #container');#container是正在获取的内容的选择器

(参见 jquery load documentation )

关于javascript - 过滤新内容以仅显示特定的 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16305271/

相关文章:

javascript - 将 React 模板放入一个全局变量的属性中

jquery - 在星级评分系统中悬停时设置星星颜色

javascript - 将 PUT 方法与 ajax Knockout js 一起使用

jquery - 文本输入字段作为 jQuery 日期选择器默认日期

打印二叉树左 View 的 JavaScript 实现返回错误结果

javascript - 如何通过 es6 import 导入 nodejs 模块 "module"以使用 "require"创建 "createRequire"函数以使用 Node 的 "require"?

javascript - 在 Firefox 中检查 IndexedDB 的真实可用性

javascript - 去除特定屏幕宽度下的JS功能

javascript - 单击重复菜单更改类名

javascript - 检测选择了多少个文件输入,并在达到限制后切换 div