【问题标题】:Add an "active" class to hyperlink using PHP使用 PHP 将“活动”类添加到超链接
【发布时间】:2010-11-02 21:32:53
【问题描述】:

我有一个动态生成的变量,其中包含用户当前选择的城市。该字符串的格式与下面导航菜单中的href完全相同,只是没有前缀/city/

Example:
    $user_city = 'london';
    $user_city = 'edinburgh';
...

我有下面的导航菜单,我想突出显示当前选择的城市,即我想为当前选择的城市添加一个“活动”类。

<ul>
    <li class="first"><a href="/city/all">All</a></li>
    <li><a href="/city/london">London</a></li>
    <li><a href="/city/liverpool">Liverpool</a></li>
    <li><a href="/city/edinburgh">Edinburgh</a></li>
    <li class="last"><a href="/city/glasgow">Glasgow</a></li>
</ul>

我知道我可以通过将每个列表项放入 if 语句来查看 $user_city 是否等于 href 属性中的字符串来实现这一点。但我想一定有更聪明的方法?

提前致谢

【问题讨论】:

  • 可以修改生成列表的函数吗?如果是这样,你能显示它的来源吗?菜单结构是否总是如上所示,或者与 li item 和结构存在偏差?菜单也是变量还是完整文档的一部分?
  • 不,我不能修改它。您所有的答案都很好,因为城市列表不会有太大变化,我很乐意接受。非常感谢大家。

标签: php html


【解决方案1】:

如果你可以使用菜单生成功能,你可以这样做:

$cities   = array('London', 'Liverpool', 'Edingburgh', 'Glasgow');
$userCity = 'London';
echo '<ul><li class="first"><a href="/city/all">All</a></li>', PHP_EOL;
foreach($cities as $city) {
    printf(
        '<li><a href="/city/%s"%s>%s</a></li>%s',
        strtolower(htmlentities($city)),
        $userCity === $city ? ' class="active' : '', // conditional
        (htmlentities($city)),
        PHP_EOL
    );
}
echo '</ul>';

换句话说,添加一个条件来检查存储在$userCity 中的当前城市是否是当前打印在屏幕上的城市,如果是,则为其添加一个类。

如果您使用菜单生成功能更新您的问题,请给我评论。我将向您展示如何使用条件修改它(如果您现在无法从上面计算出来)。


如果您必须在生成菜单后对其进行修改,并且可以确定结构始终相同,那么您可以简单地str_replace

$menu = 'a string containing your menu HTML';    
$city = 'london';
echo str_replace(
    sprintf('<li><a href="/city/%s">', $city),
    sprintf('<li><a href="/city/%s" class="active">', $city),
    $menu
);

如果您需要更难的字符串替换,consider using an HTML parser

【讨论】:

    【解决方案2】:

    检查每个链接并不是最好的方法。您可以使用 jQuery 对其进行优化:

    $(document).ready(function(){
       $('a [href="/city/<?= $user_city ?>"]').addClass('active');
    });
    

    【讨论】:

      【解决方案3】:

      你可以这样尝试:

      <?php
        $cities = array('london' => '', 'edinburgh' => '');
        $cities[$user_city] = 'active';
      ?>
      
      <ul>
      <li class="first"><a href="/city/all">All</a></li>
      <li><a href="/city/london" class="<?php echo $cities['london']; ?>">London</a></li>
      <li><a href="/city/edinburgh" class="<?php echo $cities['edinburgh']; ?>">Edinburgh</a></li>
      </ul>
      

      【讨论】:

        【解决方案4】:

        这是我刚刚写的,它应该可以解决问题。但它需要修改才能与您的脚本一起使用。

        <?php
        
          function list_cities($selected_cities = array()) {
        
            // The HTML attribute
            $class = null;
        
            // A list of all the cities
            $cities = array('london', 'liverpool', 'edinburgh', 'glasgow');
        
            // Used to find the last city
            $i = 1;
        
            // An alias of the selected cities
            $chosen = array();
        
            // Put the city name in as the array's key
            foreach( $selected_cities as $city ) {
              $chosen[$city] = $city;
            }
        
            // The first link
            echo '<li class="first"><a href="/city/all">All</a></li>' . "\n";
        
            // Loop through all the cities
            foreach( $cities as $city ) {
        
              // If we are on the last item
              $class .= ($i == count($cities)) ? 'last ' : null;
        
              // If this is a chosen city
              $class .= (isset($chosen[$city])) ? 'active ' : null;
        
              // Create the list item
              echo '<li class="' . rtrim($class) . '"><a href="/city/' . $city . '">' . ucfirst($city) . '</a></li>' . "\n";
        
              // Go to the next id
              $i++;
        
              // Reset the class
              $class = null;
            }
          }
        
        ?>
        
        
        <ul>
          <?php list_cities(array('liverpool', 'glasgow')) ?>
        </ul>
        

        【讨论】:

          猜你喜欢
          • 2013-08-15
          • 1970-01-01
          • 2013-12-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-11-26
          • 2014-01-10
          • 2010-12-29
          相关资源
          最近更新 更多