有效地将一个html文件包含在另一个html文件中

时间:2015-12-03 00:36:36

标签: php html css

我有一个文件 a.html ,以及一个链接到<head>的css文件 a_style.css 。我现在想要包含第二个html文件 b.html ,它在<style>标记中包含自己的样式。具体来说, b.html a.html 中指定了我想要的页脚。我的问题是: a_style.css 文件定义的标题,边距等也适用于 b.html 。如果我在 a.html 中加入 b.html a_style.css 是否仍然适用于所包含的文件?此外,我必须在 a.html 中包含 b.html

<div class="footer">
<?php include('./project/b.html');?>
</div>

这够了吗?或者我需要在<head>中加入一些内容,类似于

<script type="text/javascript"
  src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>

(这个特殊代码来自MathJax)?我之前没有关于php的知识,并在类似问题上找到了这个片段:Insert one HTML file into another HTML file。 这对我来说还没有用。我尝试将扩展名更改为.php,但是eclipse编辑器不允许我再折叠/扩展单个标记,使脚本非常混乱。

2 个答案:

答案 0 :(得分:1)

如果你只想包含html代码,php命令global不是正确的工具。如评论中所述,它曾用于包含PHP代码。您只想读取文件并将其打印出来。所以你想要使用的是include

readfile

CSS / JS包含的位置无关紧要。大多数人将其添加到<div class="footer"> <?php readfile('./project/b.html');?> </div> - 标记中,其他人则喜欢在<head>标记的末尾添加它们。但是如果你想让它发挥作用,它并不重要。

答案 1 :(得分:1)

  

a_style.css 文件定义了标题,边距等,这些内容也适用于 b.html 。如果我在 a.html 中加入 b.html a_style.css 是否仍然适用于所包含的文件?

首先我要说的是,这一切都取决于你包含文件的位置。冲突的样式将被最后一个覆盖。让我解释一下。

假设您有三个文件:

  • a.php只会
  • a_style.css
  • b.php

场景:1

a.php 文件中包含 b.php a_style.css :< / p>

<强> a.php只会

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Page A</title>

    <?php

        require_once("b.php");

    ?>

    <link href="a_style.css" type="text/css" rel="stylesheet" />

</head>

<body>

    <p>This is a paragraph</p>

</body>
</html>

<强> a_style.css

p{
    color: #00ff00; /* green */
}

<强> b.php

<style>
    p{
        color: #0000ff; /* blue */
    }
</style>

输出:

enter image description here

由于我们在 b.php 之后添加了 a_style.css ,因此 a_style.css 将覆盖所有冲突的样式(在本例中为段落)颜色),因此绿色段落。


场景:2

如果在 a.php 文件中包含 b.php 之前的 a_style.css <:> < / p>

<强> a.php只会

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Page A</title>

    <link href="a_style.css" type="text/css" rel="stylesheet" />

    <?php

        require_once("b.php");

    ?>

</head>

<body>

    <p>This is a paragraph</p>

</body>
</html>

<强> a_style.css

p{
    color: #00ff00; /* green */
}

<强> b.php

<style>
    p{
        color: #0000ff; /* blue */
    }
</style>

输出:

enter image description here

由于我们在 b.php 之前添加了 a_style.css ,因此 b.php 会覆盖所有冲突的样式(在本例中为段落)颜色),因此是蓝色段落。