doudeng2016 2017-06-22 19:35 采纳率: 0%
浏览 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条)

报告相同问题?

悬赏问题

  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题