简体   繁体   English

Bootstrap 导航栏可用于我的所有 html 页面

[英]Bootstrap nav-bar available to all my html pages

I am building out some front end html, css and js to display information passed through websockets.我正在构建一些前端 html、css 和 js 来显示通过 websockets 传递的信息。 At this stage, I am currently manually typing all of the html pages by hand, and referencing the pages together through button clicks.在这个阶段,我目前正在手动输入所有 html 页面,并通过单击按钮将这些页面一起引用。 However, after doing some reading on the internet, I believe there is a better way of doing this.但是,在互联网上进行了一些阅读后,我相信有更好的方法可以做到这一点。 Specifically, I would like bootstrap's nav-bar element to be shared across all of my pages.具体来说,我希望引导程序的导航栏元素在我的所有页面中共享。 However, at present, I have to manually type the code in to each of my html pages 10+ for the nav bar.但是,目前,我必须手动将代码输入到导航栏的每个 10+ html 页面中。 There has to be a better way of doing this that is more extensible and scale-able.必须有一种更好的方法来做到这一点,它更具可扩展性和可扩展性。

You might say, "Yes I know what will fix it, use node.js backend and react front end".您可能会说,“是的,我知道如何解决它,使用 node.js 后端并响应前端”。 The only problem with this however, is that the project which I am working on requires a solutions that can work without CDN's.然而,唯一的问题是我正在处理的项目需要一个可以在没有 CDN 的情况下工作的解决方案。 In addition, it must be as lightweight as possible.此外,它必须尽可能轻巧。

Is there some easier way/library of doing this in js which I am missing?是否有一些更简单的方法/库可以在我缺少的 js 中执行此操作? I am quite new to web development, however I do have experience in embedded systems.我对 Web 开发很陌生,但我确实有嵌入式系统的经验。

If you are trying to add the same content on different pages you need to create an element with the same id, create a external .js file with the content you want to repeat, and call it from the bottom of the page.如果您尝试在不同的页面上添加相同的内容,您需要创建一个具有相同 id 的元素,使用您想要重复的内容创建一个外部 .js 文件,并从页面底部调用它。

General example:一般示例:

<div id="demo"></div> <!-- this where the content would be. Create this div on different pages and embed the script inorder to get content. -->
<script src="demo.js"></script>

demo.js file: 
<script>
var nav = // the content - example of bootstrap's nav
'<nav class="navbar navbar-expand-lg navbar-light bg-light">'+
'  <a class="navbar-brand" href="#">Navbar</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">'+
'      <a class="nav-item nav-link active" href="#">Home <span class="sr-only">(current)</span></a>' +
'      <a class="nav-item nav-link" href="#">Features</a>' +
'      <a class="nav-item nav-link" href="#">Pricing</a>' +
'      <a class="nav-item nav-link disabled" href="#">Disabled</a>' +
'    </div>' +
'  </div>' +
'</nav>';

var demo = document.getElementById('demo'); // target element.
demo.insertAdjacentHTML('beforeend', nav); // add content.
</script>

Read this for more possibilities, Hope that help.阅读本文了解更多可能性,希望有所帮助。

If you are developing on visual studio then try making master page which will act as parent page for all your page.如果您在 Visual Studio 上进行开发,请尝试制作母版页,该母版页将充当您所有页面的父页面。


And if not, then use Bootstrap 4 classes which makes really easy to create the navigation bar如果没有,则使用 Bootstrap 4 类,这使得创建导航栏变得非常容易

note: Download the bootstrap 4 offline files rather than adding it via cdn to make it load faster注意:下载 bootstrap 4 离线文件而不是通过 CDN 添加它以使其加载更快

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

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