我在一个网站上工作,当用户选择要导航到的页面时,我想更改页面内容。我试图实现的是像ASP.NET一样,只有一个导航组件在所有页面上使用,当用户选择其他页面时,页面内容会更改。如果我正在用HTML/CSS和PHP构建一个网站,我将如何实现这一点。我得到的任何信息都是如何从PHP动态更改页面内容。我想更改目录中其他文件的页面内容
easy,您将创建没有导航的普通页面,并使用它们的普通链接,然后创建导航以将其作为组件添加。
nav.php
<nav>
<ul>
...
</ul>
</nav>
index.php
<?php include_once "nav.php"; ?>
<p>index</p>
contacts.php
<?php include_once "contacts.php"; ?>
<p>contacts</p>
编辑
如果你包含了太多,并且你想对它们进行简短的包含,你可以通过包含所有你想包含的文件来做到这一点,然后在任何你想要的地方包含这个文件,最好将它们添加到一个与页面分离的文件夹中,如组件或包含,如
包括/css_files.php
<link rel="stylesheet" href="...">
<link rel="stylesheet" href="...">
<link rel="stylesheet" href="...">
<link rel="stylesheet" href="...">
<link rel="stylesheet" href="...">
<link rel="stylesheet" href="...">
包括/metas.php
<meta bla="" bla="" bla="">
<meta bla="" bla="" bla="">
<meta bla="" bla="" bla="">
<meta bla="" bla="" bla="">
包括/seo.php
<meta keywords="StackOverflow, HTML">
<meta keywords="StackOverflow, HTML">
<meta keywords="StackOverflow, HTML">
对于includes.php文件,您必须选择:
- 是将包含文件和页面放在同一目录中
includes.php
<?php
include "includes/seo.php";
include "includes/metas.php";
include "includes/css_files.php";
- 您可以将其与同一目录中的其他include一起删除,但不要删除include之前的
includes/
。includes.php
include "includes/seo.php";
include "includes/metas.php";
include "includes/css_files.php";
那么页面将像一样
<head><?php include "includes.php"; ?></head>
<p>...</p>
因为包含在PHP中包括代码COPY&过去,所以你会像在纸上写一样对待它。例如,index.php就像你没有在文件名之前写includes/
一样
<head>
<?php
include "seo.php";
include "metas.php";
include "css_files.php";
?>
</head>
index lorem ipsum
并且它不会包含它,所以include也不会只获取文件并将其按原样放置在文件中,而不会更改任何
您可以使用PHP include函数仅从一个文件中控制元素。在你的index.php中,在你想放置标题的地方写下这篇文章<?php include('pathheader.php'); ?>
并且在header.php
中写入类似的代码
<header>
...
</header>
您可以将css、javascript、jquery资源文件添加到index.php
中
include代码的优点是,当访问者查看您的页面源代码或开发工具的源代码时,它将显示为您的header.php文件,而不是php include行。