问题
I had a question about letting clicked buttons/lists/anchors stay in a different colour... This one is answered by FAngel, for which I thank again. Now, I have a different problem regarding the same issue.
http://jsfiddle.net/fxTQL/7/
In this fiddle, the effect is the exact effect as I want, but, on my website, it won't work. I guess I know what the problem is, but I don't know how to solve it. My site has a frame in which topics/posts are fetched if I click on the buttons which you can see on the jsfiddle. So the page "refreshes" and shows the links. This however, makes it impossible to let the clicked links in a different colour. It goes automatically back to the first list item, which has already a background-color
How could I solve this problem?
This is something I considered.
<ul id="quick-index-list">
<li<? if(!$_GET['t']||$_GET['t']=='') echo ' class="active"';?>>
<a target="left" href="left">all</a>
</li>
回答1:
I see you're using PHP, so you can retrieve the "t" variable's value.
You can use it to assign the "active" class to the li
element which was selected:
<ul id="quick-index-list" class="pills slim muted">
<li<? if(!$_GET['t']||$_GET['t']=='') echo ' class="active"';?>>
<a id="qindex-popular" title="alle entries" target="left" href="left.php">alles</a>
</li>
<li<? if($_GET['t']=='tod') echo ' class="active"';?>>
<a id="qindex-today" title="entries van vandaag" target="left" href="left.php?t=tod">vandaag</a>
</li>
<li class="yesterday<? if($_GET['t']=='yes') echo ' active"';?>">
<a id="qindex-yesterday" title="entries van gisteren" target="left" href="left.php?t=yes">gisteren</a>
</li>
<li<? if($_GET['t']=='mix') echo ' class="active"';?>>
<a id="qindex-day" title="willekeurige entries" target="left" href="left.php?t=mix">willekeurig</a>
</li>
</ul>
So the class "active" on page load is already assigned to the correct li
with:
<? if($_GET['t']=='tod') echo ' class="active"';?> /* default element */
or
<? if($_GET['t']=='tod') echo ' class="active"';?> /* anoter element */
when you already have a class on the li
, just use
<li class="yesterday<? if($_GET['t']=='yes') echo ' active"';?>">
Be careful to echo spaces before the class name or the class attribute. Hope it helps
来源:https://stackoverflow.com/questions/12427540/stay-active-on-clicking-list-with-href