网页从索引页面略有改动

时间:2013-07-09 23:40:25

标签: php javascript html web

这个问题很简单,但我一直在寻找一个小时而一无所获:

制作与主页完全相同的页面,但特定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)有不同的内容。我该怎么做呢?

5 个答案:

答案 0 :(得分:3)

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

你有几个选择。如果只是div的内容正在改变,你可以使用相同的php页面,然后使用jquery来改变div的内容,所以像

的index.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"); ?>

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

答案 1 :(得分: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

答案 2 :(得分: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>

取文件名,并根据你可以更改内容(如果只是一页,否则根据它写一个函数/类)。

答案 3 :(得分: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>

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

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

top.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">

bottom.php:

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

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

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

请记住,如果此方法位于<?php?>标记内,则需要使用echo输出html代码

希望这会有所帮助。

答案 4 :(得分: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中的$ main_content值#change

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