简体   繁体   中英

How to activate link in another html file?

Using this thread I carried out my header into the individual html file

header.html:

<div class="navbar navbar-default navbar-fixed-top" id="top" role="banner">
  <div class="container">
    <div class="navbar-header">
      <button class="navbar-toggle" type="button" data-toggle="collapse" data-target=".navbar-collapse">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
    </div>
    <div class="navbar-collapse collapse" role="navigation" style="height: auto;">
      <ul class="nav navbar-nav">
        <li><a href="/page1.html">Page1</a></li>
        <li><a href="/page2.html">Page2</a></li>
      </ul>
    </div>
  </div>
</div>

and included it in my index.html.

<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<script> 
$(function(){
  $("#navbar-header").load("header.html"); 
  $("#footer").load("footer.html"); 
});
</script>

and

<div id="navbar-header"></div>

My question is, how to apply 'active' class on selected page (page1 and page2)?

<li class="active"><a href="/page1.html">Equator Server</a></li>

You can call a jQuery Click() event on target element by adding a click event listener on source element (an element which should activate a link on another html).

$('.icon-bar-1').on('click', function(){
  $('.page-1').click(function(){/* code to perform on targetted click listener */});
});

It is possible to use jQuery's .addClass(), using specific class in the html, for example

HTML-file to add menu

<div class="class1" id="navbar-header"></div>

separate file with header

$(document).ready(function () {
        if ($( "#navbar-header" ).hasClass( "class1" )) {
            $("#id-to-add").addClass("active");
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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