使用javascript时,超链接变得无法点击

时间:2016-04-22 23:27:11

标签: javascript twitter-bootstrap navbar

当我使用javascript突出显示活动链接

时,我的引导程序Navbar无法点击

这是我的代码:

<script type="text/javascript">
          $('.nav.navbar-nav > li').on('click', function (e) {
          e.preventDefault();
          $('.nav.navbar-nav > li').removeClass('active');
          $(this).addClass('active');
          });
</script>

这是我的导航菜单

 <nav class="navbar navbar-inverse navbar-static-top" >
  <div class="container-fluid" >

  <!-- Brand and toggle get grouped for better mobile display -->
          <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
              <span class="sr-only">Toggle navigation</span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
            </button>

             <a class="navbar-brand" rel="home" href="#" title="Fidel MRP">
                <img style="max-width:85px; margin-top: -18px; background-color: grey;
                     "src="/static/img/Fidel MRP-logo.png">
            </a>
          </div>




  <!-- Collect the nav links, forms, and other content for toggling -->
  <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
    <ul class="nav navbar-nav">
        <li class="active"><a href="#">Dashboard</a></li>
        <li><a href="/customer/home/">Sales</a></li>
        <li><a href="/vendor/home/">Purchase</a></li> 
        <li><a href="/item/category/list/">Inventory</a></li> 
        <li><a href="#">Manufacture</a></li>
        <li><a href="#">Settings</a></li>
        <li class="devider"></li>
    </ul>


<ul class ="nav navbar-nav navbar-right">
  {% if request.user.is_authenticated %}
      <li ><a href="{% url 'auth_logout' %}" %>  {{user}} Logout  </a></li>
  {% else %}
  <li ><a href="{% url 'registration_register' %}" %> Register </a></li>
       {% endif %}
  <li><div class="form">
        {% if not request.user.is_authenticated and not "/accounts/login" in request.get_full_path %}

          <form class='navbar-form navbar-right' method='POST' action='{% url "auth_login" %}'>{% csrf_token %}
              <div class='form-group'>
                  <input type='text' class='form-control' name='username' placeholder='Username' /> 
              </div>
              <div class='form-group'>
                  <input type='password' class='form-control' name='password' placeholder='Password' />
              </div>
              <button type='submit' class='btn btn-default'>Login</button>
          </form>
        {% endif %}
  </li>
</ul>
</div>
  </div><!-- /.navbar-collapse -->
</div>
</nav>

问题是

我使用提供的javascript来获取onClick后活动菜单项的视觉效果。这个脚本有效,但超链接停止工作。(所以我必须禁用脚本以便能够导航)

可能是什么原因以及如何解决?

1 个答案:

答案 0 :(得分:1)

e.preventDefault();正在停止锚点的默认行为。 (导航到href)。

删除它会让浏览器在点击时导航到新页面。