douju1852 2014-01-22 23:45
浏览 16
已采纳

Php包含在.css中[关闭]

I have a menu file "menu_include.php" thats going to be included in 6 pages. I want each element to change per page. Lets say we are in "about.php" i want the "about" to change colors with no links. How can i do this

  • 写回答

1条回答 默认 最新

  • doutang3815 2014-01-22 23:55
    关注

    A simple way to do this would be to set a variable before including your menu, something like this.

    $currentPage = "about"; // change this on each page.
    include 'menu_include.php';
    

    then in your menu_include, you could do something like this with each link

    <nav role='navigation'>
      <ul>
        <li><a href="#" <?php if(isset($currentPage) && $currentPage == 'home'){ echo ' class="active" '; } ?> >Home</a></li>
        <li><a href="#" <?php if(isset($currentPage) && $currentPage == 'about'){ echo ' class="active" '; } ?> >About</a></li>
        <li><a href="#" <?php if(isset($currentPage) && $currentPage == 'clients'){ echo ' class="active" '; } ?> >Clients</a></li>
        <li><a href="#" <?php if(isset($currentPage) && $currentPage == 'contact'){ echo ' class="active" '; } ?> >Contact Us</a></li>
      </ul>
    </nav>
    

    Then you could style the .active class to change colors as you wish..

    .active {
     background:lime;
    }
    

    Edit: To answer your question, yes you will want to wrap the variable and include in php tags like this.. I didn't put them before, because I assumed you would just put them in your existing php tags.

    <?php
     $currentPage = "about"; // change this on each page.
     include 'menu_include.php';
    ?>
    

    It sounds like you might want to learn a bit more about php, or even look into using a popular CMS like Wordpress -- http://wordpress.org there are plenty of tutorials/videos that can help you, but unfortunately I can't write ALL of the code for you ;) Trying to help as much as possible.

    Update: In response to your last question you can add a hover state to your .onthepage class.

    .menu > li > a.onthepage:hover {
      color:#f00;
      font-weight:bold;
    }
    

    This way it won't change when you hover it, I expect that is what you're going for.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用