0

这个问题很简单,但我已经环顾了一个小时,什么也没找到:

制作一个与主页完全相同的页面,但特定的 div 更改了内容

示例 index.html:

<html>
<head>
    <title>title</title>
    <style type="text/css">
    /* css goes here */
    </style>
</head>
<body>
    <div id="stay">I wont change</div>
    <div id="change">I will change</div>
</body>
</html>

所以我希望能够对一个页面进行编码,以便它从索引页面继承整个 html(不复制代码),但是一个特定的 div(这里带有 id #change)具有不同的内容。我该怎么做呢?

4

5 回答 5

3

您并没有真正“继承”代码片段,但我知道您正在尝试重用页面内容。从您发布的代码中,很难准确判断更改与索引有何不同。它只是内容更改还是索引页面没有那个div?

你有几个选择。如果只是 div 的内容发生变化,您可以使用相同的 php 页面,然后使用 jquery 来更改 div 的内容,所以类似于

索引.php

<? php include("page.php"); ?>

其他页面

<? php include("page.php"); ?>
// javascript to modify div

您可以将页面分成块并根据需要包含它们,这样您就可以有一个 top.php 和一个 bottom.php,而索引页面可以

<? php include("top.php"); ?>
<? php include("bottom.php"); ?>

然后你的类似页面可以做类似的事情

<? php include("top.php"); ?>
// custom stuff here
<? php include("bottom.php"); ?>

如果这些解决方案都不起作用,您总是可以使用模板引擎来创建页面模板,尽管这对您的情况可能有点多。

于 2013-07-09T23:46:30.467 回答
0

我看到你已经标记了这个问题php所以,我会给你答案,包括 php 实现。

创建 3 页。index.php about.phpfoo.php

目标是显示一些内容,index.php但所有内容about.php

调用此页面foo.php

<html>
<head></head>
  <body>
        <p> Show this in index.php </p>
        <?php if($_SERVER['PHP_SELF'] === 'about.php'):  ?>
        <p> Show this in about.php </p>
        <?php endif;  ?>
   </body>
</html>

现在,您所要做的就是……包含foo.php在两个页面中。

于 2013-07-09T23:48:25.213 回答
0

制作您想要的页面,您可以这样做:

<html>
<head>
    <title>title</title>
    <style type="text/css">
    /* css goes here */
    </style>
</head>
<body>
    <div id="stay">I wont change</div>
    <?php
      if(basename($_SERVER['PHP_SELF'] == "other-page.php")){ ?>   

      <div id="change">I will change</div>

      <?php }else{ ?>

      <div id="change">Original div</div>

      <?php } ?>
</body>
</html>

这需要文件名并基于该名称您可以更改内容(如果仅适用于一页,否则根据该名称编写函数/类)。

于 2013-07-09T23:48:40.823 回答
0

有很多方法可以做到这一点。这里有两个,每个都有自己的优点和缺点。

首先,如果您根本不想修改页面,您可以添加一个小的 PHP 代码段,其中将包含通过 GET 变量传入的页面。例如

<html>
<head>
    <title>title</title>
    <style type="text/css">
    /* css goes here */
    </style>
</head>
<body>
    <div id="stay">I wont change</div>
    <div id="change"><?php require($_GET['page']); ?></div>
</body>
</html>

这意味着使用 URLmypage.php?page=home.php会自动将名为 home.php 的文件的内容包含到该 div 中。

另一种方法是将该页面分成 2 个部分,并将它们都包含在您使用的任何其他页面中。例如,将代码拆分为 2 个单独的文件,例如

顶部.php:

<html>
<head>
    <title>title</title>
    <style type="text/css">
    /* css goes here */
    </style>
</head>
<body>
    <div id="stay">I wont change</div>
    <div id="change">

底部.php:

</div>
</body>
</html>

然后在您的 PHP 文件中,您可以使用以下内容

require("top.php);
MY CONTENT HERE
require("bottom.php);

<?php请记住,如果它在和?>标签内,您将需要使用 echo 在此方法上输出 html 代码

希望这可以帮助。

于 2013-07-09T23:49:23.467 回答
0

你不能这样做将纯 HTML。

要在 php 中执行此操作,首先要像这样创建模板文件:(template.php)

<html>
<head>
    <title>title</title>
    <style type="text/css">
    * css goes here */
    </style>
</head>
<body>
    <div id="stay">I wont change</div>
    <div id="change"><?=$main_content?></div>
</body>
</html>

现在,假设您要创建一个“联系我”页面。

<?php
    // in contact.php
    $main_content = "Contact me at my@email.com

    include "template.php";
?>

这会将 template.php 的内容写入页面并在 div#change 中回显 $main_content 的值

现在,这通常不受欢迎,因为随着模板大小的增加,管理变量变得很困难。为了保持理智,请使用模板引擎,因为所有其他答案都建议。

于 2013-07-09T23:50:06.407 回答