简体   繁体   English

如何动态加载外部页面内容

[英]How to dynamically load external page content

How and what would be the best way to go about dynamically loading page content? 如何动态加载页面内容的最佳方法是什么? (load new content without whole page reload / refresh using div container ) (加载新内容而无需使用div容器重新加载/刷新整个页面)

I have a gallery page and want to load only the new gallery content from other pages I will be making in the future. 我有一个图库页面,只想从以后我要制作的其他页面中加载新的图库内容。 I will be using the pagination bar to load the content. 我将使用分页栏加载内容。 Preferably without hashtags in address bar. 最好在地址栏中没有井号。 I'd appreciate any help. 我将不胜感激。 Thanks. 谢谢。

Here is the gallery page I'm working on. 这是我正在处理的图库页面。

http://www.hlipublishing.com/nmg/index.html http://www.hlipublishing.com/nmg/index.html

Below are examples of what I'm looking for and caught my attention. 以下是我正在寻找并引起我注意的示例。

http://www.unheap.com/ http://www.unheap.com/

Rethinking Dynamic Page Replacing Content and Doc Template from CSS-Tricks. 重新思考CSS技巧中的动态页面替换内容和文档模板。

Try using ajax: 尝试使用ajax:

$.ajax({
     url: url,
     data: prameters,
     dataType: "html",
     type: "post"
     success: function(result){
     //append your dynamic image here
      $("#gallery").html(result);
     }
});

On the success part, the result should return the html for your images. 成功部分, result应返回图像的html Then you're free to append it anywhere on your page. 然后,您可以随意将其附加到页面上的任何位置。 This whole process won't reload your page or add hashtags to your URL(unless you used an <a> tag as trigger to your ajax) 这整个过程不会重新加载页面或将标签添加到URL(除非您将<a>标签用作触发给ajax)

$.ajax() is your answer. $ .ajax()是您的答案。 Basically, what you want to do is: 基本上,您想做的是:

  1. Write a script that will return JUST the inner data (what goes inside section.portolfio ) for one page. 编写一个脚本,将仅返回一页的内部数据( section.portolfio内部的内容)。 Ideally, you can use a function to check if the call is from AJAX, and return only the inner data for ajax, or the full page for normal requests. 理想情况下,您可以使用函数检查调用是否来自AJAX,并仅返回ajax的内部数据,或返回正常请求的完整页面。
  2. Write a .click function for your pager that will grab the page # you want to view (from a rel tag, href tag, pretty much anything) 为您的传呼器编写一个.click函数,它将抓取您要查看的页面#(从rel标签,href标签,几乎所有内容中)
  3. Within the .click function call $.ajax() with the url for your script, the data for the page, and a success function to handle the return (which should accept the returned data in the format you specify. I would guess 'html' . This function should clear out the current content and refill the section with the new content. 在.click函数中,调用$ .ajax()并带有脚本的url ,页面的data以及用于处理返回值的success函数(该函数应接受您指定格式的返回数据。我猜是'html' 。此功能应清除当前内容,并用新内容重新填充该部分。

As an aside, one problem with your current filtering system is that you only filter the content from the page you're on, meaning you can end up with less than a full page of results even when there are more results. 顺便说一句,当前的过滤系统存在一个问题,即您仅从所在页面中过滤内容,这意味着即使有更多结果,最终结果也可能少于整页。 If you expand your function to also accept filter parameters, you can pass both the filter and page in the AJAX request to achieve a nicer user experience. 如果将功能扩展为也接受过滤器参数,则可以在AJAX请求中同时传递过滤器和页面,以获得更好的用户体验。

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

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