一个href不会去其他页面

时间:2016-01-26 07:06:24

标签: javascript jquery html twitter-bootstrap hyperlink

我有这个导航菜单,它与指向同一个index.html的不同部分的链接很少,例如href="#about"href="#product"但是当我放置href="reservation.html"时,它不会转到那个页面。换句话说,所有同页内部锚链接都有效,但外部链接(到其他页面)不起作用。任何想法为什么? 我正在使用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> 

3 个答案:

答案 0 :(得分:1)

您需要了解相对路径

/ root

./当前目录

../当前目录的父级。

试试这个,

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

答案 1 :(得分:1)

好的问题解决了,似乎在这个单页模板的index.html的底部有这一行:

$(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;
    });
  });

默认情况下会阻止我猜测的链接,所以我添加了一个if语句:

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

答案 2 :(得分:0)

如果确切链接为href="/reservation.html",请尝试使用www.website.com/reservation.html