doudeng2016 2017-06-22 19:35
浏览 72
已采纳

添加活动链接类

I have a dynamic navigation bar that stores the pages/URLs in a database. The nav.php is the script that handles displaying and running through the SQL queries to display the links. I'm having a difficult time adding a script that adds an active class to the links.

My page URLs are as follows:

  • /page/2/full-service-fleet
  • /rate-request
  • /employment
  • /page/5/links
  • /page/6/contact

PHP:

<nav>
  <?php

    while ($row = $result->fetch_assoc()) 
        {
            $navid = $row['id'];
            $navname = $row['nav'];
            $navslug = $row['slug'];
            $navurl = $row['url'];
            $navnum = $row['num'];

            if ($navurl != ''){
                $navlink = $navurl;
            } 
            else{
                $navlink = "page.php?id=".$navid."&title=".$navslug;
            }

            if (substr($navlink,0,4) != "http"){
                if ($server_name <> "") { 
                    $navlink = "http://".$server_name."/".$navlink;
                }
            }

            if ($navurl == '#'){
                $navlink = $navurl;
            }

            if ($navnum ==0){

        ?>

         <a href="<?php echo $navlink; ?>"><?php echo $navname; ?></a>
         <?php

            }else{
    ?>
    <!-- the rest isn't necessary -->
    ......
</nav>

Browser Rendered Output

The actual links that are being displayed is this line: <a href="<?php echo $navlink; ?>"><?php echo $navname; ?></a>

JS Script:

jQuery(document).ready(function($){
  // Get current path and find target link
  var path = window.location.pathname.split("/").pop();

  // Account for home page with empty path
  if ( path == '' ) {
    path = 'index.php';
  }

  var target = $('nav a[href="'+path+'"]');
  // Add active class to target link
  target.addClass('active');
});
  • 写回答

2条回答 默认 最新

  • duanhong1985 2017-06-22 19:43
    关注

    You can use this code as it is or you can grab some idea from this one :

    <div class="w3-top" style="z-index: 999;">
    <div class="navcontainer">
        <div class="w3-hide-small">
            <a href="" title="Home" class="w3-left w3-wide w3-hide-small"><img src="/images/-logo.png" alt="U.S. Transportation" title="" style=""></a>
            <div class="abovenav w3-right">
                <button class="w3-button w3-round-large" onclick="">Customer Login</button>&nbsp;
                <button class="w3-button w3-round-large" onclick="">Carrier Login</button>
            </div>
        </div>
        <div class="w3-bar nav w3-card-2 w3-left-align w3-large" style="text-overflow: auto;" id="nav">
            <div class="w3-hide-large w3-hide-medium">
                <a class="w3-hide-medium w3-hide-large w3-right w3-margin-right w3-text-white buttons" href="javascript:void(0);" onclick="myFunction()" title="Toggle Navigation Menu"><span class="fa fa-bars"></span></a>
                <a href="<?php
                if ($server_name <> "") {
                    echo "http://" . $server_name . "/";
                }
                ?>/index.php" title="Home" class="w3-left w3-margin-left w3-text-white buttons"><span class="fa fa-home w3-xlarge"></span></a>
            </div>
            <div style="margin-right: 10%;">
                <nav>
                    <?php
                    include 'yortal\db.php';
                    $sql = "SELECT *,(select count(*) from pages where parent = p.id) as num from pages p where parent = 0 and status = 'ON' order by sort DESC";
                    $result = $mysqli->query($sql);
    
                    while ($row = $result->fetch_assoc()) {
                        $navid = $row['id'];
                        $navname = $row['nav'];
                        $navslug = $row['slug'];
                        $navurl = $row['url'];
                        $navnum = $row['num'];
                        if ($navurl != '') {
                            $navlink = $navurl;
                        } else {
                            $navlink = "page.php?id=" . $navid . "&title=" . $navslug;
                        }
                        if (substr($navlink, 0, 4) != "http") {
                            if ($server_name <> "") {
                                $navlink = "http://" . $server_name . "/" . $navlink;
                            }
                        }
                        if ($navurl == '#') {
                            $navlink = $navurl;
                        }
                        if ($navnum == 0) {
                            $current_link = $_SERVER[REQUEST_URI];
                            $active_class = ($navlink==$current_link)?'active':'';
                            ?>
                            <a href="<?php echo $navlink; ?>" class="<?php echo $active_class; ?>" data-test="<?php echo $navlink.'=='.$current_link; ?>" class="w3-hide-small w3-right w3-text-white w3-padding-large buttons w3-margin-right" style="text-decoration: none; font-weight: bold;"><?php echo $navname; ?></a>
                            <?php
                        } else {
                            ?>
                            <div class="w3-dropdown-hover w3-hide-small w3-right" onclick="javascript:window.location.href = '<?php echo $navlink; ?>'">
                                <button class="w3-button"><?php echo $navname; ?></button>
                                <div class="w3-dropdown-content w3-white w3-card-4">
                                    <?php
                                    //Subpages
                                    $sql2 = "SELECT *,(select count(*) from pages where parent = p.id) as num from pages p where parent = " . $navid . " and status = 'ON' order by sort";
                                    $result2 = $mysqli->query($sql2);
                                    while ($row2 = $result2->fetch_assoc()) {
                                        $dropid = $row2['id'];
                                        $dropname = $row2['nav'];
                                        $dropurl = $row2['url'];
                                        $dropslug = $row2['slug'];
                                        $dropnum = $row2['num'];
                                        if ($dropurl != '') {
                                            $droplink = $dropurl;
                                        } else {
                                            $droplink = "page.php?id=" . $dropid . "&title=" . $dropslug;
                                        }
                                        if (substr($droplink, 0, 4) != "http") {
                                            if ($server_name <> "") {
                                                $droplink = "http://" . $server_name . "/" . $droplink;
                                            }
                                        }
                                        if ($dropurl == '#') {
                                            $droplink = $dropurl;
                                        }
                                        ?>
                                        <?php
                                        $actual_link = "http://".$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
                                        $active_class = ($actual_link==$droplink || $_SERVER[REQUEST_URI] == $droplink)?'active':'';
                                        ?>
                                        <a href="<?php echo $droplink;?>" class="<?php echo $active_class; ?>" data-test="<?php echo $actual_link.'=='.$droplink .'||'. $_SERVER[REQUEST_URI].' == '.$droplink; ?>"><?php echo $dropname; ?></a>
                                        <?php
                                    }
                                    ?>
                                </div>
                            </div><?php
                        }
                    }
                    ?>
                </nav>
            </div>
        </div>
    </div>
    

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

报告相同问题?

悬赏问题

  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂