jQuery .load 可以追加而不是替换吗?
我安装了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您无法使用
jQuery.load()
方法附加内容,但您可以使用jQuery.get()
:You can't append content using the
jQuery.load()
method, but you can do what you want usingjQuery.get()
:我可能会这样做。
#articles > *
将所有直接子项获取到#article
。I'd probably do it like this.
#articles > *
fetches all immediate children to#article
.对于附加数据,我认为您不想使用 $.load()。相反,您可能想考虑使用 $.ajax()。
For appending data, I don't think you would want to use $.load(). Instead, you might want to look into using $.ajax().
试试这个,它对我有用。
Try this it worked for me.