2017-06-19 11 views
0

を通じて、私はタブの活性化は、ナビゲーション

<div class="tslcNav" role="navigation"> 
     <div class="navbar-collapse collapse"> 
      <ul class="nav navbar-nav"> 
       <li><a href="index.html">Home</a></li> 
       <li><a href="mngPost.html">Manage Portfolio</a></li> 
       <li><a href="riskAssesment.html">Risk Assessment</a></li> 
       <li><a href="security_setup.html">Security Setup</a></li> 
       <li><a href="dataOwner-riskAssessmentCompliance.html"style="visibility: hidden"></a></li> 
      </ul> 
     </div> 
    </div> 

また

<script type="text/javascript"> 
    $(function() { 
     var pgurl = window.location.href.substr(window.location.href 
       .lastIndexOf("/") + 1); 
     $(".tslcNav ul li a").each(
       function() { 
        if(pgurl=="dataOwner-riskAssessmentCompliance.html") 
         $("riskAssesment.html").addClass("active"); 
        if ($(this).attr("href") == pgurl 
          || $(this).attr("href") == '') 
         $(this).closest('li').addClass("active"); 
       })   
    }); 

ようなコードを書いています。しかし、私はdataOwner-riskAssessmentCompliance.html page.Itの午前ときにタブをアクティブにされていませんdataOwner-riskAssessmentCompliance.htmlページにあるときにタブriskAssesment.htmlをアクティブにする必要があります。このコードで何が問題になっていますか?あなたは間違った選択を使用している

答えて

0

、あなたは、のようなページ名でhrefを使用

if(pgurl=="dataOwner-riskAssessmentCompliance.html") 
    $("[href='riskAssesment.html']").addClass("active"); 

そして、私はあなたのために、親li

$(function() { 
    var pgurl = window.location.href.substr(window.location.href 
      .lastIndexOf("/") + 1); 
    $(".tslcNav ul li a").each(function() { 
     if(pgurl=="dataOwner-riskAssessmentCompliance.html"){ 
      $("[href='riskAssesment.html']").addClass("active") 
              .closest('li').addClass('active'); 
     } 
     else { 
      if ($(this).attr("href") == pgurl || $(this).attr('href') == '') 
       $(this).closest('li') // get the closest li 
         .addClass("active"); // add active class to that li 
     } 
    }); 
}); 
+0

私は他の方法を使うことができますか?URLを使ってページに直接着くと、アクティブなタブが表示されません。そうでなければ表示されます。 URLを入力するとアクティブなタブは表示されません) – Aishwarya

+0

私の答えのコードは、ページが更新された場合にのみ機能します。アンカーをアクティブにしたい場合は、そのイベントに同じロジックを使用します。 –

0

activeクラスを追加したいと思うはずですhref

として
$("[href='riskAssesment.html']").addClass("active"); 
としてアクセスする必要があるクラスを追加する0
0

あなたは、私はそれがあなたの役に立てば幸いこの

var pgurl = window.location.href.substr(window.location.href.lastIndexOf("/") + 1); 
$('ul.nav > li').removeClass("active"); 
$("a[href='"+pgurl+"']").parent().addClass("active"); 
0
$(document).ready(function() { 
      var url = window.location.toString().split('?'); 

      $('ul.nav li a').filter(function() { 
       if (this.href != '') { 
        if (this.href == url[0]) { 
         $(this).parent().addClass('active'); 
        } 
       } 
      }); 
     }); 

を必要としています。

関連する問題