我怎么能有功能,load()除了我想附加数据而不是替换.也许我可以使用,get()但我想#posts从加载的数据中提取元素
当我做一个alert(data)我得到...
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script src="jquery.infinitescroll.js"></script>
<script>
</script>
</head>
<body>
<div id="info"></div>
<div id="posts">
<div class="post"> ... </div>
...
<ul id="pageNav" class="clearfix">
<li><a href="page1.html">1</a></li>
<li><a href="page2.html">2</a></li>
<li><a href="page3.html">3</a></li>
<li><a href="page4.html">4</a></li>
<li><a href="page5.html">5</a></li>
<li><a href="page3.html" class="next">next</a></li>
</ul>
Run Code Online (Sandbox Code Playgroud)
完整的代码可以在@ pastebin找到
use*_*716 12
没有理由你无法提取你想要的元素$.get().
$.get('test.html',function(data) {
var posts = $(data).find('#posts');
// If the #posts element is at the top level of the data,
// you'll need to use .filter() instead.
// var posts = $(data).filter('#posts');
$('#container').append(posts);
});
Run Code Online (Sandbox Code Playgroud)
编辑:
您可能没有注意到上面的代码注释,所以我将在这里更明确地说明.
如果#posts元素位于层次结构的顶部,data换句话说,如果它没有父元素,则需要使用.filter().
$.get('test.html',function(data) {
var posts = $(data).filter('#posts');
$('#container').append(posts);
});
Run Code Online (Sandbox Code Playgroud)
编辑:
根据以下评论,您似乎需要.filter()而不是.find().
原因是你传递的是整个HTML结构.当你这样做时,jQuery将body标记的直接子节点作为数组放在jQuery对象中.
jQuery .filter()仅针对该数组中的节点进行过滤.不是他们的孩子.
jQuery 在数组中节点.find()的后代之间进行搜索.
因此,您需要同时使用它们..filter()在顶部(#posts)获取正确的一个并.find()获得正确的后代(.next).
$(data).filter('#posts').find('.next');
Run Code Online (Sandbox Code Playgroud)
这会将设置缩小为仅#posts元素,然后查找.next作为后代的元素.