简体   繁体   English

跨网站的单个导航栏?

[英]Single navigation bar across website?

I am currently building a web application for my employer, but I have some restrictions.我目前正在为我的雇主构建一个 Web 应用程序,但我有一些限制。 It has to be in HTML, CSS, and Javascript for the front end, and I have to use a lot of different pages for this, each sharing the same navigation bar.前端必须使用 HTML、CSS 和 Javascript,为此我必须使用许多不同的页面,每个页面共享相同的导航栏。

If the navigation is the same for each page, is it possible to write the navigation bar once and use it across the entire website?如果每个页面的导航都相同,是否可以编写一次导航栏并在整个网站上使用它? I am just annoyed when I make a change to a link or something and I have to run through and change each pages respective navigation link.当我更改链接或其他内容时,我很生气,我必须遍历并更改每个页面各自的导航链接。 Normally I'd use something like Angular to achieve this, but I am not sure how to do it with this more barebones approach.通常我会使用 Angular 之类的东西来实现这一点,但我不确定如何使用这种更准系统的方法来做到这一点。 They really don't use any JS libraries either so if there's a way to do it with "raw" HTML CSS and JS I'd love to learn how this works if it exists.他们也真的不使用任何 JS 库,所以如果有办法用“原始”HTML CSS 和 JS 来做到这一点,我很想了解它是如何工作的(如果它存在的话)。

JQuery查询

As JQuery is JS-based, you might be allowed to use it.由于 JQuery 是基于 JS 的,因此您可能会被允许使用它。 You could then add a navigation div into each page's template:然后,您可以将导航 div 添加到每个页面的模板中:

<div id="navigation"></div>

and include a script on each page that executes the following JQuery-code:并在每个页面上包含一个执行以下 JQuery 代码的脚本:

$(function() {
    $("#navigation").load("navigation.html");
});

Pure JavaScript纯 JavaScript

Alternatively, if you cannot use JQuery whatsoever, you could use plain JavaScript, which is more lightweight but not as clean.或者,如果您不能使用 JQuery,您可以使用纯 JavaScript,它更轻量级但不那么干净。

Wherever you want to include the navigation, simply include a script:无论您想在何处包含导航,只需包含一个脚本:

<script src="nav.js"></script>

Which holds your navigation based on document.write :其中包含基于document.write的导航:

document.write('<div>\
    ... your navigation content ...\
    </div>\
');

The easiest way would just be to write the code in a JS file, and include that code on your pages.最简单的方法就是在 JS 文件中编写代码,然后将该代码包含在您的页面中。 You can hard code it or make it more intelligent, but here's a basic demo.您可以对其进行硬编码或使其更智能,但这里有一个基本的演示。

 var html = '<ul>\\ <li>\\ <a href="#">link</a>\\ </li>\\ <li>\\ <a href="#">link</a>\\ </li>\\ <li>\\ <a href="#">link</a>\\ </li>\\ </ul>'; document.getElementById('nav').innerHTML = html;
 <nav id="nav"></nav>

You can include html files inside html with the w3 library.您可以使用 w3 库将html文件包含在 html 中。

<div w3-include-html="navbar.html"></div>

<script>
    w3.includeHTML();
</script>

Check out this w3schools link .查看此 w3schools链接

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

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