a href is not going to other pages

2019-09-17 10:35发布

问题:

I have this navigation menu that has few links to different part of the same index.html like a href="#about" or a href="#product" but when i put a href="reservation.html" it does not go to that page. in other word, all same-page internal anchor link works but external links (to other pages) does not work. any idea why? I am using bootstrap:

<nav class="navbar navbar-default navbar-fixed-top">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>                        
      </button>
      <a href="index.html"><img src="image/logo.png"></a>
    </div>
    <div class="collapse navbar-collapse" id="myNavbar">
      <ul class="nav navbar-nav navbar-right" id="font">
        <li><a href="#about">ABOUT</a></li>
        <li><a href="#services">SERVICES</a></li>
        <li><a href="#portfolio">PORTFOLIO</a></li>
        <li><a href="#pricing">PRICING</a></li>
        <li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown" href="#">EVENTS <span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="#">Wedding</a></li>
            <li><a href="#">Bachelor Party</a></li>
            <li><a href="#">Prom</a></li>
          </ul>
        </li>
        <li><a href="reservation.html">RESERVATION</a></li>
        <li><a href="#contact">CONTACT</a></li>

      </ul>
    </div>
  </div>
</nav> 

回答1:

You need to know about relative path

/ root

./ current directory

../ parent of the current directory.

Try this,

<a href="./index.html"><img src="image/logo.png"></a>


回答2:

OK problem solved it seems that at the bottom of the index.html of this one-page template there is this line:

$(document).ready(function(){
  // Add smooth scrolling to all links in navbar + footer link
  $(".navbar a, footer a[href='#myPage']").on('click', function(event) {

    // Prevent default anchor click behavior
    event.preventDefault();

    // Store hash
    var hash = this.hash;

    // Using jQuery's animate() method to add smooth page scroll
    // The optional number (900) specifies the number of milliseconds it takes to scroll to the specified area
    $('html, body').animate({
      scrollTop: $(hash).offset().top
    }, 900, function(){

      // Add hash (#) to URL when done scrolling (default click behavior)
      window.location.hash = hash;
    });
  });

the links as i guessed are prevented by default so i added an if statement saying:

 if (".navbar a" =! ".external"){here goes the rest of li that have external links and now it works}


回答3:

Try using href="/reservation.html" if the exact link is www.website.com/reservation.html.