doulangchao8934 2018-07-03 14:37
浏览 48

如何为这段代码添加活动选择器[重复]

This question already has an answer here:

Can anyone help me with adding an active selector so that when a link is clicked that link will receive a different color background or maybe just bold it?

<div class="section group">
  <div class="left-nav col span_1_of_2_">
    <ul style="color:#333;">
      <a class="link-leftbar" href="www.test.com"><li>Registration</li></a>
      <a class="link-leftbar" href="www.test.com"><li>Status</li></a>
      <a class="link-leftbar" href="www.test.com"><li>Instruction</li></a>
      <a class="link-leftbar" href="www.test.com"><li>Links</li></a>
      <a class="link-leftbar" href="www.test.com"><li>Policy</li></a>
      <a class="link-leftbar" href="www.test.com"><li>Troubleshooting</li></a>
      <a class="link-leftbar" href="www.test.com"><li>International</li></a>
      <a class="link-leftbar" href="www.test.com"><li>Shipping</li></a>
      <a class="link-leftbar" href="www.test.com"><li>Promotions</li></a>
      <a class="link-leftbar" href="www.test.com"><li>Payment</li></a>
      <a class="link-leftbar" href="www.test.com"><li>Contacts</li></a>
      <a class="link-leftbar" href="www.test.com"><li>Terms</li></a>
    </ul>
  </div>
</div>
</div>
  • 写回答

1条回答 默认 最新

  • drcx71276 2018-07-12 12:07
    关注

    That is actually very simple to accomplish, and based on what you mentioned in 2 comments of yours in your other post, where you reload the page for each link and the url's looks like in the sample "oneplus" link, e.g. "https://www.oneplus.com/support", it can be set with just 1 line of script.

    By simply detect when the page is loaded (.ready()), you grab the current window.location and use its pathname property.

    Combined with the attribute selector attribute="value", you can then add a class to your link, which is the recommended way, and here is how it can look in your code

    $( document ).ready(function() {
    
      $('.link-leftbar[href$="' + window.location.pathname + '"]').addClass('active');
    
    });
    

    In the above I used the ending with ($) selector [attr$="value"], but there are more variants if your url gets more complex, and you can check them out at this link Attribute selectors.

    Since this function gets called every time page load, it will work for a page refresh too.

    If you would start using e.g. Ajax to load the content, by simply execute this line, it will set your active link.

    $('.link-leftbar[href$="' + window.location.pathname + '"]').addClass('active');
    

    Here is a sample, where I manually set the url, and also show how to alter the inline CSS, if you for any reason can't use a class.

    Also note, as it is invalid to have a li wrapped in any other element but being a child of an ul, I changed the ul/li to div/span in below sample, and added additional styling so the links are stack vertically and have bullets.

    Stack snippet

    var window_location = new URL("http://www.test.com/support/international");
    
    $(document).ready(function() {
    
      // using a class, recommended
      $('.link-leftbar[href$="' + window_location.pathname + '"]').addClass('active');
    
      // change inline CSS, if you can't use classes
      $('.link-leftbar[href$="' + window_location.pathname + '"]').css('color', 'darkblue');
    
    });
    .active {
      font-weight: bold;
    }
    
    /*  additional styling -  */
    .link-leftbar span {           /*  - stack links vertically  */
      display: block;
    }
    .link-leftbar span::before {   /*  - add a bullet to each  */
      content: '•';
      padding: 0 10px 0 25px;
      display:inline-block;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    
    <div class="section group">
      <div class="left-nav col span_1_of_2_">
        <h2 class="leftnav-title">Support</h2>
        <div>
          <a class="link-leftbar" href="www.test.com/support/manuals">
            <span>Manuals</span>
          </a>
          <a class="link-leftbar" href="www.test.com/support/international">
            <span>International</span>
          </a>
          <a class="link-leftbar" href="www.test.com/support/tracking">
            <span>Tracking</span>
          </a>
          <a class="link-leftbar" href="www.test.com/support/contacts">
            <span>Contacts</span>
          </a>
          <a class="link-leftbar" href="www.test.com/support/security">
            <span>Security</span>
          </a>
          <a class="link-leftbar" href="www.test.com/support/registration">
            <span>Registration</span>
          </a>
          <a class="link-leftbar" href="www.test.com/support/policy">
            <span>Policy</span>
          </a>
          <a class="link-leftbar" href="www.test.com/support/troubleshooting">
            <span>Troubleshooting</span>
          </a>
          <a class="link-leftbar" href="www.test.com/support/promotions">
            <span>Promotions</span>
          </a>
          <a class="link-leftbar" href="www.test.com/support/shipping">
            <span>Shipping</span>
          </a>
          <a class="link-leftbar" href="www.test.com/support/terms">
            <span>Terms</span>
          </a>
          <a class="link-leftbar" href="www.test.com/support/claim">
            <span>Claim</span>
          </a>
        </div>
      </div>
    </div>

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 蓝桥oj3931,请问我错在哪里
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染