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

发布于 2024-09-28 15:38:19 字数 1370 浏览 3 评论 0原文

我安装了 WordPress,并尝试使用 jQuery 创建“加载更多”效果。我在使用页面片段的基本 .load 功能时遇到一些问题。我不介意使用 .get,因为我在这里看到一些线程认为这是一个更好的解决方案。

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

第一页: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>

我在页面底部有一个链接,我尝试执行以下操作:

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

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

有人可以帮助我使用命令将第二页中的 .story 类简单地附加到第一页上的#articles div 中吗?之后我可以弄清楚使用变量、循环和 PHP for WordPress 实现自动化的流程。只需需要一些有关正确 jQuery 脚本的帮助。

PS 额外问题:如果有人知道 jQuery .load/.get 是否会损害浏览量(这对于那些靠广告收入的人来说很重要),请告诉我!如果每个 .load/.get 都算作 Google Analytics 的另一次点击,那就太好了!

谢谢!

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:

First Page: http://example.com/page/1/

The 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/

The 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. That means, even if this were to work, there would be recursive divs within divs resulting from each click. 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? 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. 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!

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

要走就滚别墨迹 2024-10-05 15:38:20

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

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

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");
});
星星的轨迹 2024-10-05 15:38:20

我可能会这样做。

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

#articles > * 将所有直接子项获取到 #article

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.

飞烟轻若梦 2024-10-05 15:38:20

对于附加数据,我认为您不想使用 $.load()。相反,您可能想考虑使用 $.ajax()。

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

For appending data, I don't think you would want to use $.load(). Instead, you might want to look into using $.ajax().

$.ajax({
  url: 'yourlocation/test.html',
  success: function(data) {
    $('.result').html(data);
    $("#yourdiv").append(data);
  }
});
铁憨憨 2024-10-05 15:38:20

试试这个,它对我有用。

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

Try this it worked for me.

$('#articles').append( $('<div/>').load('http://example.com/page/2/ #articles') );
完美的未来在梦里 2024-10-05 15:38:20
$.get(YourURL, function(data){ 
$('#divID').append(data);});
$.get(YourURL, function(data){ 
$('#divID').append(data);});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文