0

I'm using bootstrap as web UI. When I click one li item, it redirects to a new webpage and the active item will be the first item again, how can I sovle this problem?

    <ul id="profileTabs" class="nav nav-list">
        <li class="active"><a href="http://localhost/ojr/user/edit/Charles0429" data-toggle="list">Profile</a></li>
        <li><a href="http://localhost/ojr/user/edit/Charles0429" data-toggle="list">About Me</a></li>
        <li><a href="http://localhost/ojr/user/edit/Charles0429" data-toggle="list">My Match</a></li>
    </ul>
Charles0429
  • 1,406
  • 5
  • 15
  • 31

2 Answers2

4

You'll have to use localstorage or cookies to manage that.

$(function() { 
  //for bootstrap 3 use 'shown.bs.tab' instead of 'shown' in the next line
  $('a[data-toggle="tab"]').on('shown', function (e) {
    //save the latest tab; use cookies if you like 'em better:
    localStorage.setItem('lastTab', $(e.target).attr('id'));
  });

  //go to the latest tab, if it exists:
  var lastTab = localStorage.getItem('lastTab');
  if (lastTab) {
      $('#'+lastTab).tab('show');
  }
});

Code Courtesy How do I keep the current tab active with twitter bootstrap after a page reload?

In above code tabs are used but try replacing with li's you expect

Community
  • 1
  • 1
Vaibs_Cool
  • 6,126
  • 5
  • 28
  • 61
-1

This could be achieved with just css. You need to apply classes to the html tags on the pages and individual classes to each li item.

e.g. HTML (for the profile page)

<html class="profilePage" >
...
    <ul id="profileTabs" class="nav nav-list">
        <li class="profileMenuItem"><a href="http://localhost/ojr/user/edit/Charles0429" data-toggle="list">Profile</a></li>
        <li class="aboutMenuItem"><a href="http://localhost/ojr/user/edit/Charles0429" data-toggle="list">About Me</a></li>
        <li class="matchMenuItem"><a href="http://localhost/ojr/user/edit/Charles0429" data-toggle="list">My Match</a></li>
    </ul>
...
</html>

CSS

.profilePage .profileMenuItem,
.aboutPage .aboutMenuItem,
.matchPage .matchMenuItem {
    /*active styles here*/
}

N.b. <html class="profilePage" > would change depending on which page you are on

E.g. For the My Match page

<html class="matchPage" >

For the About Me page

<html class="aboutPage" >
Dave Haigh
  • 4,369
  • 5
  • 34
  • 56
  • Once a time, there is an only one active item. When I click one Item, it redirects to one page(may be not the current one), So how could CSS achieve my goal? – Charles0429 Sep 10 '13 at 13:03
  • I don't understand you fully. But the technique I have provided can achieve your goal as far as I understand it. Essentially - applying styles to specific elements depending on what page you are on. – Dave Haigh Sep 10 '13 at 13:50