简体   繁体   English

$()。load()而不是加载整个页面,但需要在所有页面上保留菜单

[英]$().load() instead of loading whole page, but need to keep menu on all pages

I want to reload on the content area of my website with jQuery/AJAX $().load() function. 我想用jQuery / AJAX $().load()函数重新加载我网站的内容区域。 But my problem is that the header and footer needs to be displayed on all pages no matter your entry URL. 但我的问题是,无论您的输入网址是什么, headerfooter需要显示在所有页面上。

My site is build using templates, so my first thought was to remove the output of the layout above and below the unique content, as I would then prevent ie. 我的网站是使用模板构建的,所以我首先想到的是删除布局输出的独特内容上方和下方,因为我会阻止ie。 the menu from being displayed twice. 菜单显示两次。 But I realized that if the user is not entering my site through the first page, lets say index.php, he won't ever see header or footer just an unstyled text page. 但我意识到,如果用户没有通过第一页进入我的网站,让我们说index.php,他将永远不会看到页眉或页脚只是一个没有样式的文本页面。

My question is, how would you work around this issue? 我的问题是,你将如何解决这个问题? JavaScript (+ jQuery) and HTML5 is allowed. 允许使用JavaScript(+ jQuery)和HTML5。

page.php: page.php文件:

<div id="header"></di>
<div id="mainContent"></div>
<div id="footer"></div>

js: JS:

$("#content").load("page.php #mainContent"); //that will only load mainContent

or: 要么:

$.get('page.php', function (data) {
    data = $(data).find('#mainContent').html();
    $("#content").empty().append(data);
});

For more information, see the section in the jQuery documentation on load() and page fragments 有关更多信息,请参阅有关load()和页面片段的jQuery文档中的部分

It seems like you could have a couple of different options. 看起来你可以有几个不同的选择。 If a user is visiting your site but not going to the first page, then you could just check to verify that the header and footers are showing after the page is loaded. 如果用户正在访问您的站点但没有访问第一页,那么您可以检查以确认页面加载后页眉和页脚是否显示。 If they are not found, then the initial site layout should be created. 如果找不到它们,则应创建初始站点布局。 You could even decide to build the first page (index.php) this way so that every page would be handled the same way 您甚至可以决定以这种方式构建第一页(index.php),以便以相同的方式处理每个页面

$(document).ready(function() {
    // If an element of id "header" isn't found in the DOM
    if (!$("#header").length() > 0) {
        // Generate the header and insert it as the first element of the DOM
    }
    if (!$("#footer").length() > 0) {
        // Generate the footer and append it to the last element of the DOM
    }
});

There are no doubt other solutions that you could entertain as well; 毫无疑问,您也可以享受其他解决方案; you could probably have an include on each page that only gets included if the page is requested via a GET rather than an AJAX request. 如果通过GET而不是AJAX请求请求页面,则每个页面上可能只包含一个include。

The technique that you're looking for is known as Hijax, where you intercept the response from an AJAX request and only pull out the part of the DOM that you intend to replace on the rendered page. 您正在寻找的技术称为Hijax,您可以在其中拦截来自AJAX请求的响应,并仅提取您要在呈现的页面上替换的DOM部分。

The general idea is that the actual URL's themselves will still return the full page content, so if the request is coming from a new visitor, the entire DOM will load, but if the request is coming from the user clicking a hijaxed link on your page with a CSS selector specified, then only the part of the page identified by the selector is replaced. 一般的想法是,实际的URL本身仍将返回整个页面内容,因此如果请求来自新访问者,则整个DOM将加载,但是如果请求来自用户,则单击页面上的hijaxed链接如果指定了CSS选择器,则只替换选择器标识的页面部分。

Take a look at Hijax - jQuery Plugin . 看看Hijax - jQuery插件 The actual site itself is built using the plug-in, and if you watch your network tab and look at the inspector in your Chrome or Firebug tools, you can see the content is swapped out without replacing the menus, header, or other elements that aren't being replaced. 实际站点本身是使用插件构建的,如果您在Chrome或Firebug工具中查看网络选项卡并查看检查器,则可以看到内容被换出而无需替换菜单,标题或其他元素。没有被替换。

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

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