• <bdo id='7eCFo'></bdo><ul id='7eCFo'></ul>

    1. <small id='7eCFo'></small><noframes id='7eCFo'>

      <i id='7eCFo'><tr id='7eCFo'><dt id='7eCFo'><q id='7eCFo'><span id='7eCFo'><b id='7eCFo'><form id='7eCFo'><ins id='7eCFo'></ins><ul id='7eCFo'></ul><sub id='7eCFo'></sub></form><legend id='7eCFo'></legend><bdo id='7eCFo'><pre id='7eCFo'><center id='7eCFo'></center></pre></bdo></b><th id='7eCFo'></th></span></q></dt></tr></i><div id='7eCFo'><tfoot id='7eCFo'></tfoot><dl id='7eCFo'><fieldset id='7eCFo'></fieldset></dl></div>
    2. <legend id='7eCFo'><style id='7eCFo'><dir id='7eCFo'><q id='7eCFo'></q></dir></style></legend>
      <tfoot id='7eCFo'></tfoot>

      使用 php 在菜单项上设置活动类

      时间:2023-10-11

            • <bdo id='R1boL'></bdo><ul id='R1boL'></ul>
              <tfoot id='R1boL'></tfoot>
            • <i id='R1boL'><tr id='R1boL'><dt id='R1boL'><q id='R1boL'><span id='R1boL'><b id='R1boL'><form id='R1boL'><ins id='R1boL'></ins><ul id='R1boL'></ul><sub id='R1boL'></sub></form><legend id='R1boL'></legend><bdo id='R1boL'><pre id='R1boL'><center id='R1boL'></center></pre></bdo></b><th id='R1boL'></th></span></q></dt></tr></i><div id='R1boL'><tfoot id='R1boL'></tfoot><dl id='R1boL'><fieldset id='R1boL'></fieldset></dl></div>

              <small id='R1boL'></small><noframes id='R1boL'>

                <tbody id='R1boL'></tbody>
                <legend id='R1boL'><style id='R1boL'><dir id='R1boL'><q id='R1boL'></q></dir></style></legend>
              1. 本文介绍了使用 php 在菜单项上设置活动类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个由

                • 元素和一个 class="active" 元素组成的简单菜单,用于标记当前页面.$_get[] 传递一个变量,通过 url 选择特定页面:?pg=PAGE.

                  I have a simple menu made of <ul><li> elements and a class="active" in place to mark the current page. A variable is passed by $_get[] to select the specific page by url: ?pg=PAGE.

                  我对 php 还很陌生,还在学习中.这工作得很好,但我觉得应该有一种更简单、更短的方法.

                  I am fairly new to php and still learning. This works just fine, but i feel there ought to be a simpler and shorter way.

                  <ul class="nav">
                    <li <?php if ($_GET['pg'] == "PAGE1") { echo "class="active""; } ?>><a href="?pg=PAGE1">FIRST PAGE</a></li>
                    <li <?php if ($_GET['pg'] == "PAGE2") { echo "class="active""; } ?>><a href="?pg=PAGE2">SECOND PAGE</a></li>
                  </ul>
                  

                  推荐答案

                  <?php
                      $pages = array(
                          'PAGE1' => 'FIRST PAGE',
                          'PAGE2' => 'SECOND PAGE');
                  ?>
                  
                  <ul class="nav">
                    <?php foreach ($pages as $pageId => $pageTitle): ?>
                    <li <?=(($_GET['pg'] == $pageId) ? 'class="active"' : '')?>><a href="?pg=<?=$pageId?>"><?=$pageTitle?></a></li>
                    <?php endforeach; ?>
                  </ul>
                  

                  http://php.net/manual/en/control-structures.foreach.php

                  不要重复自己——两个 li-s 非常相似,唯一的区别是页面 ID 和标题.一旦您拥有两个以上的页面,这种方法将非常有用.

                  Don't repeat yourself -- both li-s are very similar, the only difference is in page ID and title. This approach will really help once you have more than two pages.

                  尽量将 PHP 和 HTML 分开——一旦您决定将它们保存在单独的文件中(有时您会这样做),这将使您的生活更轻松.

                  Try to keep PHP and HTML as separate as possible -- this will make your life easier once you decide to keep them in separate files (and you will sometimes).

                  这篇关于使用 php 在菜单项上设置活动类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:PHP 多级菜单 下一篇:如何在 opencart 的导航菜单中添加信息链接?

                相关文章

                <i id='DhACf'><tr id='DhACf'><dt id='DhACf'><q id='DhACf'><span id='DhACf'><b id='DhACf'><form id='DhACf'><ins id='DhACf'></ins><ul id='DhACf'></ul><sub id='DhACf'></sub></form><legend id='DhACf'></legend><bdo id='DhACf'><pre id='DhACf'><center id='DhACf'></center></pre></bdo></b><th id='DhACf'></th></span></q></dt></tr></i><div id='DhACf'><tfoot id='DhACf'></tfoot><dl id='DhACf'><fieldset id='DhACf'></fieldset></dl></div>
                  <bdo id='DhACf'></bdo><ul id='DhACf'></ul>

                  <legend id='DhACf'><style id='DhACf'><dir id='DhACf'><q id='DhACf'></q></dir></style></legend>

                  <small id='DhACf'></small><noframes id='DhACf'>

                  1. <tfoot id='DhACf'></tfoot>