【问题标题】:button in header menu php标题菜单php中的按钮
【发布时间】:2025-12-04 11:15:02
【问题描述】:

我为我的网站创建了一个 header.php。 对于我的菜单,我创建了 5 个按钮。 当我按下一个按钮时,它会将我重定向到与之关联的页面上。 到目前为止一切都很好。

我想要的是,在当前页面上,与之关联的页面的按钮更改为其他颜色或背景图像。

我不知道我们能不能做到这一点,我是否能很好地解释自己。

这里是我的 header.php

<div id="main_menu">
    <div id="menu_blog"><button onclick="location.href='blog.html'"><h1>Trucs/Astuces</h1></button></div>
    <div id="menu_contact"><button onclick="location.href='/contact.php'"><h1>Contact</h1></button></div>
    <div id="menu_soumission"><button onclick="location.href='/soumission.php'"><h1>Soumission</h1></button></div>
    <div id="menu_realisation"><button onclick="location.href='/realisations.php'">
    <h1>Réalisations</h1></button></div>
    <div id="menu_service"><button onclick="location.href='/services.php'">
    <h1>Services</h1></button></div>
    <div id="menu_a_propos"><button onclick="location.href='/a_propos.php'"><h1>L'entreprise</h1></button></div>
</div>

【问题讨论】:

  • 为什么使用按钮而不是链接? - 另外,您使用的是 WordPress 之类的东西 - 还是直接使用 php?

标签: php css button header


【解决方案1】:

简单的方法是使用 CSS

#yourbutton:hover{
 background-color: red;
 }

 #yourbutton:active{
  background-color: red;
 }

【讨论】:

  • 我认为 OP 想要找出他们在哪个页面上,并在“当前”页面的链接中添加一个类。
【解决方案2】:

为每个链接传递一个 GET 值

<div id="menu_contact"><button onclick="location.href='/contact.php?active=1'"><h1>Contact</h1></button></div>

检查按钮中active的值

< button <?php if ($_GET['active']==1) echo 'class="active"'; ?> ..../>

然后为活动类制作一个css样式

.active{

  /*your style here*/
}

【讨论】:

    【解决方案3】:

    您的 PHP 页面:

    <div id="main_menu">
        <div id="menu_blog">
            <button onclick="location.href='/blog.html'" <?php if($_SERVER['REQUEST_URI'] == '/blog.html'){echo 'class="currentPage"'; }?>>
                <h1>Trucs/Astuces</h1>
            </button>
        </div>
        … and so on for each menu item ...
    </div>
    

    你的 CSS:

    button.currentPage{
        background-color: red;
    }
    

    【讨论】:

      【解决方案4】:

      短版:

      将 $current_location 添加到每个页面并为其命名

      <?php
      $current_location = "a_propos" ;
      ?>
      

      检查位置是否正确,然后更改背景颜色

      <div id="main_menu">
        <div id="menu_a_propos"><button <?php if ($current_location == "a_propos");?> style=" background-color: red;"<?php };?> onclick="location.href='/a_propos.php'"><h1>L'entreprise</h1></button></div>
      </div>
      

      【讨论】:

        最近更新 更多