0

次のjavascriptを使用してアクティブなクラスを設定しようとしています

var url = window.location.pathname,
                    urlRegExp = new RegExp(url == '/' ? window.location.origin + '/?$' : url.replace(/\/$/, '')); // create regexp to match current url pathname and remove trailing slash if present as it could collide with the link in navigation in case trailing slash wasn't present there
                // now grab every link from the navigation
                $('.page-sidebar a').each(function () {
                    // and test its normalized href against the url pathname regexp
                    if (urlRegExp.test(this.href.replace(/\/$/, ''))) {
                        $(this).addClass('active');
                    }
                });

<li>問題は、親要素ではなく親要素に設定する必要があることです<a>

HTML メニューの例

<div class="page-sidebar nav-collapse collapse">
   <ul>
      <li>
          <div class="sidebar-toggler hidden-phone"></div>
      </li>
      <li class="start"> // HERE I NEED TO ADD CLASS ACTIVE
          <a href="/default.aspx">
          <i class="icon-home"></i>
          <span class="title">Dashboard</span>
          </a>
      </li>
<ul>

4

4 に答える 4