比方说,这是你的菜单链接列表:
你可以达到你需要用一个简单的jQuery请求,另一个“模块使者” php文件是什么:
$(document).ready(function(){
$('#menulist li a').click(function(e){
e.preventDefault(); // This will prevent the link to work.
$('#contenuto').load('module_bringer.php?mod=' + $(this).attr('rel');); // You get the moule from rel attribute
// You should keep the actual link so bots can crawl.
});
});
详解编辑下面
0 - 好的。首先,你应该让所有的东西继续工作,没有js和jquery。机器人不会通过浏览器进行爬网,他们只是获取源代码并抓取代码。 (假设你关心seo)
1 - 为菜单链接添加一个额外的属性(在这种情况下,我已经添加了rel属性)。此attr的值是模块参数值。
2 - 包括jQuery库(如果你以前没有包括 - 你可以从here下载
3 - 您可以使用第二代码部分,我已经写了你只需要改变。触发部分在第一个代码块中解释菜单项时,触发器会触发jQuery代码$('#menulist li a').click您必须根据您的菜单结构更改此部分这部分是使httprequest将结果放入#contenuto div
4 - 您需要创建另一个文件来包含将成为jquery httprequest目标文件的内容(在这种情况下,我已经命名它module_bringer.php)。内容应该是这样的:
// You probably need to include some files here like db connection, class definitions etc.
?>
if(empty($_GET["modul"]) && empty($_GET['userpanel'])) {
require('moduli/news.php');
}
elseif (file_exists("moduli/" . $_GET['modul'] . ".php")) {
include("moduli/" . $_GET['modul'] . ".php");}
elseif (file_exists("moduli/userpanel/" . $_GET['userpanel'] . ".php")) {
include("moduli/userpanel/" . $_GET['userpanel'] . ".php");
}else{
include("moduli/404.php");
}
?>
这篇博客介绍如何利用jQuery的.load()方法和PHP来实现动态模块加载。通过为菜单链接添加rel属性,当点击链接时,jQuery阻止默认行为并发送一个请求到'module_bringer.php',该PHP文件根据接收到的模块参数动态加载对应内容。这种方法确保了SEO友好,同时实现了页面内容的无刷新更新。
209

被折叠的 条评论
为什么被折叠?



