【问题标题】:Include header in index?在索引中包含标题?
【发布时间】:2019-07-04 01:55:19
【问题描述】:

我有一个小问题,我有以下代码,想在我的网站上添加语言切换。 假设我有一个 header.php 和一个 index.php。 我想在 header.php 中包含按钮和脚本。 Header.php 包含在 index.php 中。如何从 index.php 访问标头中的函数?

我的代码:

在 Header.php 中

  <head>
    <style type="text/css">
      body.fr > p[lang=en] {
        display: none;
      }
      body.en > p[lang=fr] {
        display: none;
      }
    </style>
  </head>
    <button onclick="document.body.className='en'">English</button>
    <button onclick="document.body.className='fr'">French</button>

在Index.php中

                <?php
                include('header.php');
                ?>  

  <body class="en">
    <p lang="en">This is English</p>
    <p lang="fr">This is French</p>
  </body>

如果我点击按钮,文本不会改变。我假设我必须将 header.php 与 index.php 内容集成,但如何?

【问题讨论】:

  • 嗨 :) 为什么在 header.php 中有按钮?以及他们为什么退出body
  • 为什么在 header.phpindex.php 文件中都有按钮?
  • @MoshFeu 因为我不喜欢在每一页上导入按钮。我有很多页面。像 hello.php 和 world.php 一样,我希望标题中的按钮包含在所有文件中。
  • @OmariCelestine 对不起我的错误
  • @M.Antony 按钮应该在正文中,css如果是内联的,应该作为正文中的最后一个内容放置。

标签: php html indexing header


【解决方案1】:

我不确定为什么您在 header.phpindex.php 文件中都有按钮,但从 CSS 中删除 &gt; 似乎可以解决问题。另外,我建议您将代码格式化为类似于以下内容:

header.php

<head>
    <title>Languages</titke>
    <!-- Meta tags CSS and JavaScript Files -->
</head>

language_buttons.php

<button onclick="document.body.className='en'">English</button>
<button onclick="document.body.className='fr'">French</button>

footer.php

<style type="text/css">
    body.fr p[lang=en] {
        display: none;
    }
    body.en p[lang=fr] {
        display: none;
    }
</style>

index.php

<!DOCTYPE html>
<html>
    <?php include('header.php'); ?>  

    <body class="en">
        <?php include 'language_buttons.php'; ?>

        <p lang="en">This is English</p>
        <p lang="fr">This is French</p>

        <?php include 'footer.php'; ?>
    </body>
</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多