Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to change active class while click to another link in bootstrap use jquery?
    primarykey
    data
    text
    <p>I have a html as sidebar, and use <code>Bootstrap</code>.</p> <pre><code>&lt;ul class="nav nav-list"&gt; &lt;li class="active"&gt;&lt;a href="/"&gt;Link 1&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="/link2"&gt;Link 2&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="/link3"&gt;Link 3&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; </code></pre> <p>I want to do some thing:</p> <p>When I <code>click</code> a link such as Link 3, the <code>page content</code> will change to Link 3's content, and the Link 3 will have class <code>active</code>, and remove the active clss in Link 1.</p> <p>I think this can be implemented in <code>jQuery</code>, and I have searched many question in SO, such as:</p> <ul> <li><a href="https://stackoverflow.com/questions/13224855/how-to-keep-an-active-link-in-nav-list-twitter-bootstrap">How to keep an active link in nav list (twitter bootstrap)?</a></li> <li><a href="https://stackoverflow.com/questions/9301507/bootstrap-twitter-css-active-navigation">Bootstrap CSS Active Navigation</a></li> </ul> <p>I use this script:</p> <pre><code>$(document).ready(function () { $('.nav li').click(function(e) { $('.nav li').removeClass('active'); var $this = $(this); if (!$this.hasClass('active')) { $this.addClass('active'); } //e.preventDefault(); }); }); </code></pre> <p>But most of them use <code>preventDefault</code>, this will prevent the continued action. So it can't change to Link 3's page content.</p> <p>If I remove the preventDefault statement, when the page load Link 3's content, the active class will back to Link 1.</p> <p>So Is there any way to solve this problem?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload