Javascript 如何使用jquery更改引导中的.active类
我正在使用django,并尝试创建一个基本模板。我想更改导航栏链接中指向单独链接的.active类, 例如,navbar中的Home链接指向Home.html,navbar中的About Us链接指向About.html 我还使用了jquery中的一段代码来实现这一点,但它不起作用。请建议做这件事的任何方法 请不要打扰我,我是新手Javascript 如何使用jquery更改引导中的.active类,javascript,jquery,html,twitter-bootstrap,django-templates,Javascript,Jquery,Html,Twitter Bootstrap,Django Templates,我正在使用django,并尝试创建一个基本模板。我想更改导航栏链接中指向单独链接的.active类, 例如,navbar中的Home链接指向Home.html,navbar中的About Us链接指向About.html 我还使用了jquery中的一段代码来实现这一点,但它不起作用。请建议做这件事的任何方法 请不要打扰我,我是新手 <!DOCTYPE html> <html> <head> {% load staticfiles %} <
<!DOCTYPE html>
<html>
<head>
{% load staticfiles %}
<meta charset="UTF-8">
<title>{% block title %}{% endblock %} | Vasu Genset Pvt. Ltd.</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://bootswatch.com/paper/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="{% static 'index/css/index.css' %}">
<link rel="stylesheet" href="{% static 'index/css/products-style.css' %}">
{% block style %}{% endblock %}
</head>
<body>
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="/home/" >Vasu Genset Pvt. Ltd.</a>
<button class="navbar-toggle collapsed" data-toggle="collapse" data-target="#mydropdown">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="collapse navbar-collapse" id="mydropdown">
<ul class="nav navbar-nav navbar-right" id="navbar-change">
<li class="active"><a href="/home/">Home</a></li>
<li><a href="/aboutus/">About Us</a></li>
<li class="dropdown">
<a href="/productsandservices/" class="dropdown-toggle" data-toggle="dropdown">Products & Services <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="/generatorrepair/">Generator Repair</a></li>
<li><a href="/generatormaintenance/">Generator Maintenance</a></li>
<li><a href="/alternator/">Alternator Repair & Maintenance</a></li>
<li><a href="/amc/">AMC Service Provider</a></li>
<li><a href="/spareparts/">Spare Parts</a></li>
<li><a href="/generatorrentalhire/">Generator Rental & Hire</a></li>
<li><a href="/synchronizing/">Synchronizing Panel</a></li>
<li><a href="/amfpanel/">AMF Panel</a></li>
<li><a href="acousticenclosures">Acoustic Enclosures</a></li>
</ul>
</li>
<li><a href="/contactus/">Contact Us</a></li>
</ul>
</div>
</div>
</nav>
{% block body %}
{% endblock %}
<footer class="text-center">
<div class="container-fluid">
<div class="row">
<div class="col-xs-offset-1 col-xs-10 col-md-offset-3 col-md-6">
<h4>Copyright © Vasu Genset Pvt. Ltd.</h4>
</div>
</div>
</div>
</footer>
</body>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="{% static 'index/js/bootstrap.min.js' %}"></script>
<script>
$(document).ready(function () {
$('.nav li').click(function(e) {
$('.nav li').removeClass('active');
var $this = $(this);
if (!$this.hasClass('active')) {
$this.addClass('active');
}
//e.preventDefault();
});
});
</script>
</html>
{%load staticfiles%}
{%block title%}{%endblock%}瓦苏发电机组私人有限公司。
{%block style%}{%endblock%}
-
{%block body%}
{%endblock%}
版权及副本;瓦苏发电机组私人有限公司。
$(文档).ready(函数(){
$('.nav li')。单击(函数(e){
$('.nav li').removeClass('active');
var$this=$(this);
if(!$this.hasClass('active')){
$this.addClass('active');
}
//e、 预防默认值();
});
});
您的代码可以工作,但它将在x-nested ul li中触发x时间(取消对该行的注释,以便在控制台中查看单击时发生的情况)。根据您期望的行为,只要停止事件传播就可以做到这一点。(加上if(!$this.hasClass('active'))
将始终返回true,因为您刚刚删除了nav中包含的所有li中的所有类“active”)
$('.nav li')。单击(函数(e){
//log(e.currentTarget,“//”,e.target,“/”,this);
e、 停止传播();
$('.nav li').removeClass('active');
$(this.addClass('active');
});代码>
.active{
背景色:红色;
}
-
请查看,希望这会有所帮助。