简体   繁体   English

jQuery .load可以追加而不是替换吗?

[英]Can jQuery .load append instead of replace?

I have a WordPress install and I'm trying to use jQuery to create a Load More effect. 我有一个WordPress安装,我正在尝试使用jQuery创建一个Load More效果。 I'm having some trouble using the basic .load feature for page fragments. 我在使用页面片段的基本.load功能时遇到了一些麻烦。 I don't mind using .get as I saw some threads here regarding that as a better solution. 我不介意使用.get,因为我在这里看到一些线程,认为这是一个更好的解决方案。

Here's my page URL structure and the contents: 这是我的页面URL结构和内容:

First Page: http://example.com/page/1/ 第一页: http://example.com/page/1/http://example.com/page/1/

The HTML: HTML:

<div id="articles">
  <div class="story">blah blah blah</div>
  <div class="story">blah blah blah</div>
  <div class="story">blah blah blah</div>
  <div class="story">blah blah blah</div>
</div>

Second Page: http://example.com/page/2/ 第二页: http://example.com/page/2/http://example.com/page/2/

The HTML: HTML:

<div id="articles">
  <div class="story">blah blah blah</div>
  <div class="story">blah blah blah</div>
  <div class="story">blah blah blah</div>
  <div class="story">blah blah blah</div>
</div>

I have a link at the bottom of the page and I've tried doing the following: 我在页面底部有一个链接,我尝试过以下操作:

jQuery(#articles).load('http://example.com/page/2/ #articles');

Sadly, there are 2 issues. 可悲的是,有两个问题。 First the .load function grabs the #articles div and its contents from the second page and places it into the existing #articles div . 首先,.load函数从第二页抓取#articles div及其内容,并将其放入现有的#articles div That means, even if this were to work, there would be recursive divs within divs resulting from each click. 这意味着,即使这是有效的,每次点击都会产生div内的递归div。 It's just messy. 这太乱了。

Could someone help me with the command to simply append the .story classes from the second page into the #articles div on the first page? 有人可以帮我命令,只需将第二页的.story classes附加到第一页的#articles div吗? I can figure out the logistics of automating it with variables, loops, and PHP for WordPress after. 我可以用WordPress之后的变量,循环和PHP来计算自动化的后勤。 Just need some assitance with the proper jQuery script. 只需要适当的jQuery脚本一些帮助。

PS Bonus Question: If anyone knows whether jQuery .load/.get will harm pageviews, which is important for those with advertising-based revenue, please let me know! PS Bonus问题:如果有人知道jQuery .load / .get是否会损害浏览量,这对于那些有广告收入的人来说非常重要,请告诉我们! If each .load/.get counts as another hit for Google Analytics, we're good! 如果每个.load / .get都算作Google Analytics的另一个点击,我们就会很好!

Thanks! 谢谢!

You can't append content using the jQuery.load() method, but you can do what you want using jQuery.get() : 您不能使用jQuery.load()方法追加内容,但您可以使用jQuery.get()

$.get('/page/2/', function(data){ 
  $(data).find("#articles .story").appendTo("#articles");
});

I'd probably do it like this. 我可能会这样做。

$('#articles').append($('<div/>', { id: '#articles-page2' });
$('#articles-page2').load('http://example.com/page/2/ #articles > *');

#articles > * fetches all immediate children to #article . #articles > *所有直接孩子提取到#article

试试这个对我有用。

$('#articles').append( $('<div/>').load('http://example.com/page/2/ #articles') );

For appending data, I don't think you would want to use $.load(). 为了追加数据,我认为你不想使用$ .load()。 Instead, you might want to look into using $.ajax(). 相反,您可能希望使用$ .ajax()。

$.ajax({
  url: 'yourlocation/test.html',
  success: function(data) {
    $('.result').html(data);
    $("#yourdiv").append(data);
  }
});
$.get(YourURL, function(data){ 
$('#divID').append(data);});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM