duanchuang1935 2013-09-19 11:07
浏览 44
已采纳

PHP - 如何更改链接的类

I am trying to learn PHP for a website that I am building. In CSS, I have a class, nav a.thispage, setup to make a 'button' on the navigation be the same color as the highlight. It works beautifully. But, as I added pages, I find that I needed to constantly update all of the HTML files of the site, over, and over again. I found out that PHP could help me to automate this. I use the following PHP, in my HTML to do this:

 <?php include 'content/header.php';?>

The header.php file has the following content:

<header>
  <h1><img id="headerimage" src="Images/GrandLodge.png"/>Lodge</h1>
  <nav>
    <ul>
      <li><a href="index.php">Home</a></li>
      <li><a href="events.php">Events</a></li>
      <li><a href="social.php">Social</a></li>
      <li><a href="about.php">About</a></li>
      <li><a href="contact.php">Contact</a></li>
      <li><a href="gamestores.php">Area Game Stores</a></li>
      <li><a href="http://someaddress" target="_blank">PFS</a></li>
    </ul>
  </nav>
</header>

Now, because I am using this method, I can't just set the class="thispage" on the a tag. Is there a way to set the class, dynamically, with PHP? If so, how to I tell if the page loading the html is actually the page that needs it? Is using PHP even the correct way to handle this, or should I be using JavaScript?

I know this is a lot, and I didn't really provide a lot of what I have done, but I can't actually seem to see what I need to do for this. All I really need is a point in the right direction, rather than a full code sample.

Thank you for any help you can provide.

  • 写回答

3条回答 默认 最新

  • dtrz99313 2013-09-19 11:33
    关注

    You can use a bit oF PHP and JavaScript to handle this

    <header>
       <h1><img id="headerimage" src="Images/GrandLodge.png"/>Lodge</h1>
       <nav>
         <ul>
           <li><a id="aHome" href="index.php">Home</a></li>
           <li><a id="aEvents" href="events.php">Events</a></li>
           <li><a id="aSocial" href="social.php">Social</a></li>
           <li><a id="aAbout" href="about.php">About</a></li>
           <li><a id="aContact" href="contact.php">Contact</a></li>
           <li><a id="aGame" href="gamestores.php">Area Game Stores</a></li>
           <li><a id="aPFS" href="http://someaddress" target="_blank">PFS</a></li>
         </ul>
       </nav>
     </header>
    

    Now suppose the visitor has landed on the About page. The code to check that and set the class name on the a tag would be

     <?php
        if ($_SERVER['SCRIPT_NAME'] == '/about.php') { 
     ?>
     <script type="text/javascript">
       document.getElementById("aAbout").className = 'thispage';
     </script>
     <?php
       }
     ?>
    

    Try it and let me know.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP