简体   繁体   English

使用 Javascript 的导航栏中的遍历问题(跨应用程序共享)

[英]Traversal issue in navigation(which is shared across application) bar using Javascript

Since my application had same navigation bar across application, I thought of implementing shared navigation bar across application.由于我的应用程序在应用程序之间具有相同的导航栏,因此我想在应用程序之间实现共享导航栏。

My project structure is a follows
    Login
    |-Login.html, Login.css, Login.js
    Home
    |-Home.html, Home.css, Home.js
    Test
    |-Project.html, Project.css, Project.js
         Test1
            |-Test1.html, Test1.css, Test1.js
         Test2
            |-Test2.html, Test2.css, Test2.js
    |-Temp
        |-Temp.html, Temp.css, Temp.js
    |-Shared
        |-Shared.html, Shared.css, Shared.js, navigation.html

Here is my navigation.html which I have kept inside shared folder.这是我保存在共享文件夹中的导航。html。

 <nav class="navbar navbar-expand-lg navbar-light bg-light"> <div class="collapse navbar-collapse" id="navbarNav"> <ul class="navbar-nav"> <li class="nav-item "> <a class="nav-link" href="../Home/Home.html" >Home</a> </li> <li class="nav-item"> <a class="nav-link" href="../Employee/CreateEmployee.html" >Employee</a> </li> <li class="nav-item dropdown"> <a class="nav-link dropdown-toggle" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" >Test</a> <div class="dropdown-menu" aria-labelledby="navbarDropdown"> <a class="dropdown-item" href="../Test/Test1/NewCQ.html" >Create New CQ</a> <a class="dropdown-item" href="../Test/Test2/Test2.html" >Update CQ</a> </div> </li> <li class="nav-item"> <a class="nav-link" href="../Temp/Temp.html" >Temp</a> </li> </ul> <ul class="navbar-nav ml-auto"> <li class="nav-item"> <a class="nav-link float-right" href="../Login/Login.html" >Logout</a> </li> </ul> </div> </nav>

This is how I have created div in respective html files这就是我在各自的 html 文件中创建 div 的方式

 <div class="row border-bottom border-dark"> <div class="col px-0"> <!--Navigation bar--> <div id="navigationBar"> </div> </div> </div>

When respective page visits I am loading that navigation.html file in shared.js from shared folder which I have imported in all the pages.当各个页面访问时,我正在从我在所有页面中导入的共享文件夹中加载 shared.js 中的 navigation.html 文件。

//Load navigation.html from shared folder
$(function(){
    $("#navigationBar").load("../Shared/navigation.html");
});

The issue here is that这里的问题是

  1. relative paths will be different from different pages, so how do i need to provide the relative paths inside navigation.html不同页面的相对路径会有所不同,所以我需要如何在导航中提供相对路径。html
  2. $("#navigationBar").load("../Shared/navigation.html"); $("#navigationBar").load("../Shared/navigation.html"); is not loading inside Test.html(its beacuse of relative path) when redirect from home.html从 home.html 重定向时,未在 Test.html 中加载(其原因是相对路径)

please let me know if am doing anything wrong here.如果我在这里做错了什么,请告诉我。 waiting for your responses.等待您的回复。 Thanks in advance.提前致谢。

So, why use relative paths at all?那么,为什么要使用相对路径呢? Just use an absolute path in your links.只需在链接中使用绝对路径。

<a class="dropdown-item" href="/folder/Test/Test2/Test2.html">Update CQ</a>

You'll need to have some more javascript detecting the current path and figuring out the relative path from there, but that's a lot of work to do for no real gain unless you have a very specific reason to use relative paths.您需要更多的 javascript 来检测当前路径并从那里找出相对路径,但除非您有非常具体的理由使用相对路径,否则需要做很多工作却没有真正的收获。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM