如何使网站的导航栏出现在每个页面上?对我来说简直不起作用。

huangapple go评论133阅读模式
英文:

How can I have the nav bar on every page of a website? It is simply not working for me

问题

以下是您要翻译的内容:

"can someone please help me get the nav bar on any page
i tried every method but none of them work
here is the nav bar"

"this SHOULD work but it comes up as blank. Very annoying"

"sorry the first file is called 'navbar.html'"

英文:

can someone please help me get the nav bar on any page
i tried every mehotd but none of them work
here is the nav bar

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
        
    <link rel="stylesheet" href="style.css">
    <script src="https://kit.fontawesome.com/0a48c3a8e0.js" crossorigin="anonymous"></script>

</head>
<div class="navbar">
    <a class="active" href="home.html"> Home </a>
    <a href="about.html">About</a>
    <a href="start">Start</a>
    <a href="blog">Blog</a>
    <div class="dropdown">
        <button class="dropbutton"> Interests</button>
        <div class="dropdown-content">
                <a href="finance">Finance</a>
                <a href="music">Music</a>
                <a href="language">Language</a>
                <a href="math">Math</a>
        </div>
    </div>

    <div class="search-container">
    <input type="text" placeholder="Search...">
    <button type="submit"><i class="fa-sharp fa-solid fa-magnifying-glass"></i></button>
    </div>

</div>
</html>
<!-- https://stackoverflow.com/questions/31954089/how-can-i-reuse-a-navigation-bar-on-multiple-pages -->

this SHOULD work but it comes up as blank. Very annoying

<!DOCTYPE html>
<html>

    <div id="nav-placeholder"> </div>

    <script>
     $(function() {
         $("#nav-placeholder").load("navbar.html")
     });
     </script>


</html>

sorry the first file is called "navbar.html"

答案1

得分: 0

尝试这个,我已经重新排列了您的代码。关键是,您只需要将导航栏 div 包含在外部文件中,然后在 DOM 准备就绪时调用 .load jQuery 函数,以便可以插入数据。请注意,这将导致每次加载页面时都会额外调用服务器。其他替代方法包括使用静态站点捆绑器,以便在构建阶段执行此操作,或者使用诸如React、Vue等框架,但根据您的总体用例,这可能过于繁琐。

index.html

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
  <script src="https://kit.fontawesome.com/0a48c3a8e0.js" crossorigin="anonymous"></script>
  <script>
    $(document).ready(function() {
      $("#nav-placeholder").load("navbar.html")
    })
  </script>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <div id="nav-placeholder"></div>
</body>
</html>

navbar.html

<div class="navbar">
  <a class="active" href="home.html"> Home </a>
  <a href="about.html">About</a>
  <a href="start">Start</a>
  <a href="blog">Blog</a>
  <div class="dropdown">
    <button class="dropbutton">Interests</button>
    <div class="dropdown-content">
      <a href="finance">Finance</a>
      <a href="music">Music</a>
      <a href="language">Language</a>
      <a href="math">Math</a>
    </div>
  </div>
  <div class="search-container">
    <input type="text" placeholder="Search...">
    <button type="submit"><i class="fa-sharp fa-solid fa-magnifying-glass"></i></button>
  </div>
</div>

Code Sandbox 链接 - 由于没有 styles.css,CSS 是错误的,但一旦您包含它,它应该被正确格式化。

编辑 - 更改了HTML

英文:

Try this, I have rearranged your code.
The key is that you need only to include the navbar div in the external file and then call the .load jQuery function once the DOM is ready so that the data can be inserted. Be aware that this will result in an additional call to the server each time a page is loaded.
Alternative ways include using a static site bundler so that this is performed during the build stage or to use a framework such as React, Vue, etc, however this may be overkill depending on your overall use-case.

index.html

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
  &lt;meta charset=&quot;utf-8&quot;&gt;
  &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;
  &lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js&quot;&gt;&lt;/script&gt;
  &lt;script src=&quot;https://kit.fontawesome.com/0a48c3a8e0.js&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
  &lt;script&gt;
	$(document).ready(function() {
		$(&quot;#nav-placeholder&quot;).load(&quot;navbar.html&quot;)
	})
  &lt;/script&gt;
  &lt;link rel=&quot;stylesheet&quot; href=&quot;styles.css&quot;&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;div id=&quot;nav-placeholder&quot;&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

navbar.html

&lt;div class=&quot;navbar&quot;&gt;
  &lt;a class=&quot;active&quot; href=&quot;home.html&quot;&gt; Home &lt;/a&gt;
  &lt;a href=&quot;about.html&quot;&gt;About&lt;/a&gt;
  &lt;a href=&quot;start&quot;&gt;Start&lt;/a&gt;
  &lt;a href=&quot;blog&quot;&gt;Blog&lt;/a&gt;
  &lt;div class=&quot;dropdown&quot;&gt;
    &lt;button class=&quot;dropbutton&quot;&gt;Interests&lt;/button&gt;
    &lt;div class=&quot;dropdown-content&quot;&gt;
      &lt;a href=&quot;finance&quot;&gt;Finance&lt;/a&gt;
      &lt;a href=&quot;music&quot;&gt;Music&lt;/a&gt;
      &lt;a href=&quot;language&quot;&gt;Language&lt;/a&gt;
      &lt;a href=&quot;math&quot;&gt;Math&lt;/a&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div class=&quot;search-container&quot;&gt;
    &lt;input type=&quot;text&quot; placeholder=&quot;Search...&quot;&gt;
    &lt;button type=&quot;submit&quot;&gt;&lt;i class=&quot;fa-sharp fa-solid fa-magnifying-glass&quot;&gt;&lt;/i&gt;&lt;/button&gt;
  &lt;/div&gt;
&lt;/div&gt;

Code Sandbox Link - since there is no styles.css the CSS is wrong but once your include that it should be formatted correctly.

Edit - changed HTML

huangapple
  • 本文由 发表于 2023年3月12日 07:03:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75710118.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定