如何在选定的页面上添加动态活动类 [英] how to add dynamic Active class on selected page

查看:144
本文介绍了如何在选定的页面上添加动态活动类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://hotelshimlahill.com/demo/
本网站bootstrap,
包括导航栏的标题部分来自一个名为'header.php'的单独文件
现在我想为用户访问的每个页面添加活动类。
我怎么能这样做?请帮助我。

http://hotelshimlahill.com/demo/ this website I've made in bootstrap, the header section including nav bar is coming from a separate file called 'header.php' Now i want to add active class for each page on which user is visiting. how can i do that? Kindly assist me. Thankyou.

推荐答案

在您的个人页面,例如 about.php services.php ,您可以:

In your individual page such as about.php, services.php, you can do:

$currentPage = 'about'; // current page is about, do the same for other page
include('header.php');

然后在 header.php 可以检查:

<ul class="nav navbar-nav">
    <li class="<?php if($currentPage =='about'){echo 'active';}?>" ><a href="index.php">Home</a></li>
    <li class="<?php if($currentPage =='services'){echo 'active';}?>" ><a href="about.php">about</a></li>
</ul>

或者您也可以尝试使用jQuery:

Or you can try to use jQuery as well:

$(document).ready(function($){
    var url = window.location.href;
    $('.nav li a[href="'+url+'"]').addClass('active');
});

这篇关于如何在选定的页面上添加动态活动类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆