0

I want to use Bootstrap in my new ASP.Net MVC project, as you know the structure of Navbar is like:

<div class="navbar">
  <div class="navbar-inner">
    <a class="brand" href="#">Title</a>
    <ul class="nav">
      <li class="active"><a href="#">Home</a></li>
      <li><a href="#">Link</a></li>
      <li><a href="#">Link</a></li>
    </ul>
  </div>
</div>  

as you see the active Url have active class. I want to know how can I sense the active url and get it active class?

I use MVC 3 with Razor.

4

2 に答える 2

2

アクティブなアイテムを設定したいということですか?(「アクティブクラスを取得する」と言うとき)

この他のほぼ同じ質問の回答を参照してください。それは仕事をします(ただし、最適なコードではありません)。

<script type="text/javascript">
    $(document).ready(function () {
        var url = window.location.pathname;
        var substr = url.split('/');
        var urlaspx = substr[substr.length-1];
        $('.nav').find('.active').removeClass('active');
        $('.nav li a').each(function () {
            if (this.href.indexOf(urlaspx) >= 0) {
                $(this).parent().addClass('active');
            }
        });
    });
</script>
于 2013-01-10T10:42:42.183 に答える
-3

JQueryを使用してこれを行うことができます。

$(document).ready(function(){ 
  alert( $('.nav').children('.active').html() );
}
于 2012-12-22T01:33:03.587 に答える