简体   繁体   English

如何激活另一个HTML文件中的链接?

[英]How to activate link in another html file?

Using this thread I carried out my header into the individual html file 使用线程,我将标头导入了单独的html文件

header.html: 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. 并将其包含在我的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)? 我的问题是,如何在所选页面(第1页和第2页)上应用“活动”类?

<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). 您可以通过在源元素(应激活另一个html上的链接的元素)上添加click事件侦听器来在目标元素上调用jQuery Click()事件。

$('.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中的特定类来使用jQuery的.addClass()。

HTML-file to add menu HTML文件添加菜单

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

separate file with header 带标题的单独文件

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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