簡體   English   中英

添加活動鏈接類

[英]Add Active Link Class

我有一個動態導航欄,用於將頁面/ URL存儲在數據庫中。 nav.php是處理顯示和運行SQL查詢以顯示鏈接的腳本。 我很難添加一個腳本,為鏈接添加一個活動類。

我的網頁網址如下:

  • / /第2頁/全服務隊
  • /速率請求
  • /就業
  • /頁/ 5 /鏈接
  • /頁/ 6 /接觸

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>

瀏覽器渲染輸出

顯示的實際鏈接是以下行: <a href="<?php echo $navlink; ?>"><?php echo $navname; ?></a> <a href="<?php echo $navlink; ?>"><?php echo $navname; ?></a>

JS腳本:

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');
});

您可以按原樣使用此代碼,也可以從這個中獲取一些想法:

<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>

首先,我很確定你可能走錯path 我建議以下實現取決於each()

$(document).ready(function(){
   loc = window.location.pathname;
   $("nav a").each(function(){
      if ($(this).attr("href") == loc){
         $(this).addClass("active");
         $(this).attr("href") = "#"; // to prevent link to the same page
         return true;
      }
   });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM