为了在页面中加载更多内容,我使用 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
是正在获取的内容的选择器
关于javascript - 过滤新内容以仅显示特定的 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16305271/