gpt4 book ai didi

javascript - 更改引导导航栏中的事件类不保留在继承的 Django 模板中

转载 作者:行者123 更新时间:2023-12-01 02:06:53 27 4
gpt4 key购买 nike

我的导航栏位于 base.html 模板中,该模板由所有页面扩展。当我单击导航栏中的链接时,会添加事件类,但当我访问该页面后,当仅突出显示初始页面时,它会恢复到之前的状态。

这是我的 base.html 模板

{% load static %}
<!-- DOCTYPE html -->
<html>
<head>
<title>Title</title>
<!-- new bootstrap -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">



<script src="https://use.fontawesome.com/a0c7be9623.js"></script>

<!-- css -->
<link rel='stylesheet' href='{% static "css/base.css" %}' />

</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light fixed-top">
<a class="navbar-brand" href='{% url "home" %}'>Maximum Likelihood</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavAltMarkup">
<div class="navbar-nav mr-auto">
<a class="nav-item nav-link active" href='{% url "home" %}'>Info<span class="sr-only">(current)</span></a>
<a class="nav-item nav-link" href='{% url "about" %}'>About</a>
<a class="nav-item nav-link" href='{% url "stuff" %}'>Stuff</a>
</div>
<div class="navbar-nav">
<a class="nav-item nav-link ml-auto" href='google.com'>Outside link</a>
</div>
</div>
</nav>


{% block content %}
{% endblock content %}

<br>


<!-- <!-- bootstrap's js stuff -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js" integrity="sha384-cs/chFZiN24E4KMATLdqdvsezGxaGsi4hLGOzlXwp5UZB1LY//20VyM2taTB4QvJ" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js" integrity="sha384-uefMccjFJAIv6A+rW+L4AHf99KvxDjWSu1z9VI8SKNVmz4sk7buKt/6v9KI65qnm" crossorigin="anonymous"></script>
<script src='{% static "js/nav.js" %}'/></script>
</body>

这是我的 nav.js

$(".nav-link").on("click", function(){
$(".nav").find(".active").removeClass("active");
$(this).addClass("active");
});

最佳答案

最好的方法是使用django block more info

base.html

<!-- codes here -->
<div class="navbar-nav mr-auto">
<a class="nav-item nav-link {% block nav_item_home %}{% endblock nav_item_home %}" href='{% url "home" %}'>Book Materials <span class="sr-only">(current)</span></a>
<a class="nav-item nav-link {% block nav_item_about %}{% endblock nav_item_about %}" href='{% url "about" %}'>About</a>
<a class="nav-item nav-link {% block nav_item_stuff %}{% endblock nav_item_stuff %}" href='{% url "stuff" %}'>Stuff</a>
</div>

当你在继承模板中时

当然,只需将其添加到页面顶部的extends之后即可。
假设我们位于“关于”页面:

{% extends 'base.html' %}
{% block nav_item_about %}active{% endblock nav_item_about %}

关于javascript - 更改引导导航栏中的事件类不保留在继承的 Django 模板中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50028673/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com