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

问题描述:

我安装了WordPress,并且正在尝试使用jQuery创建加载更多效果。使用基本.load功能处理页面片段时遇到了一些麻烦。我不介意使用.get,因为我在这里看到一些线程可以将其作为一种更好的解决方案。

I have a WordPress install and I'm trying to use jQuery to create a Load More effect. I'm having some trouble using the basic .load feature for page fragments. I don't mind using .get as I saw some threads here regarding that as a better solution.

这是我的页面网址结构和内容:

Here's my page URL structure and the contents:

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

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>

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

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');

遗憾的是,有2个问题。首先,.load函数从第二页获取 #articles div 及其内容,并将其放入现有的 #articles div 。这意味着,即使这可行,每次点击也会在div内产生递归div。

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. That means, even if this were to work, there would be recursive divs within divs resulting from each click. It's just messy.

有人可以帮助我简单地从第二页附加 .story类的命令吗?进入首页的 #articles div ?之后,我可以弄清楚使用变量,循环和适用于WordPress的PHP自动化的后勤工作。只需使用适当的jQuery脚本即可获得帮助。

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? I can figure out the logistics of automating it with variables, loops, and PHP for WordPress after. Just need some assitance with the proper jQuery script.

P.S。奖励问题:如果有人知道jQuery .load / .get是否会损害综合浏览量,这对于那些基于广告收入的用户来说很重要,请告诉我!如果每个.load / .get都算是Google Analytics(分析)的另一个热门,那么我们很好!

P.S. 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! If each .load/.get counts as another hit for Google Analytics, we're good!

谢谢!

您不能使用 jQuery.load()方法附加内容,但是可以使用 jQuery.get()

You can't append content using the jQuery.load() method, but you can do what you want using jQuery.get():

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