I've written a cookie-based toggle show/hide menu. It remembers what was open/closed using the jQuery cookie plugin. It exactly works fine; the only problem is I have to add different script for each menu. So each menu has its own script.
This is the code so far:
jQuery
$(document).ready(function() {
var panel = $("#f1_fold");
var button = $("#f1_fold_i");
var initialState = "expanded";
var activeClass = "hidden";
var visibleText = "<img src='html/sys-img/min.gif' alt='-' />";
var hiddenText = "<img src='html/sys-img/plus.gif' alt='+' />";
if($.cookie("panelState1") == undefined) {
$.cookie("panelState1", initialState);
}
var state = $.cookie("panelState1");
if(state == "collapsed") {
panel.hide();
button.html(hiddenText);
button.addClass(activeClass);
}
button.click(function(){
if($.cookie("panelState1") == "expanded") {
$.cookie("panelState1", "collapsed");
button.html(hiddenText);
button.addClass(activeClass);
} else {
$.cookie("panelState1", "expanded");
button.html(visibleText);
button.removeClass(activeClass);
}
panel.slideToggle("1000");
return false;
});
});
And here is the HTML snippet
<div class="tableborder">
<div class='maintitle'>
<div style='float: right; width: auto !important; padding-right: 6px; padding-left: 3px;'><a id='f1_fold_i' id='1' href='#' style='vertical-align:middle; border:0px solid #000; cursor:pointer;' /><img src='html/sys-img/min.gif' alt='-' /></a></div>
<div><img src='style_images/1/nav_m.gif' border='0' alt='>' width='8' height='8' /> <a href="index.php?c=1">Gotei 13</a></div>
</div>
<span id='f1_fold' style='display:;'>
<table width="100%" border="0" cellspacing="1" cellpadding="4">
<tr>
<th align="left" colspan="2" width="59%" class='titlemedium'>Forum</th>
<th align="right" width="14%" class='titlemedium'>Stats</th>
<th align="left" width开发者_如何转开发="25%" class='titlemedium'>Last Post Info</th>
</tr>
<tr>
<td class="row4" align="center"><img src='subnonew.gif' border='0' alt='No New Posts' /></td>
<td class="row4"><b class="dtitle"><a href="index.php?showforum=1">Rules & Announcement Board</a></b><br /><span class="desc">Pengumuman, panduan, dan peraturan forum dipajang di sini. Baru daftar? Yuk tengok dulu sebelum posting~<br /></span></td>
<td class="row2" align="right">58 Topics <br />1410 Replies</td>
<td class="row2" nowrap="nowrap">
<a href='index.php?showtopic=2069&view=getlastpost' title='Go to last post'><img src='http://209.85.62.23/style_images/1/lastpost.gif' border='0' alt='Last Post' /></a>
Today at 01:59 am<br />In: <a href='index.php?showtopic=2069&view=getnewpost' title='[INFO] Gelar Jepang UI 2011 (Go to first unread)'>[INFO] Gelar Jepang UI 2011</a><br />By: <a href='index.php?showuser=1'>Xaliber</a> <br />Thread: <a href='index.php?showforum=36'>Gathering & Events</a></td>
</tr> </table></span><table width="100%"><tr>
<td class='darkrow2' colspan="5"> </td>
</tr>
</table>
</div>
Notice that I have to add different script for each menu because it depends on the f1_fold
and f1_fold_i
css ID. For the other menu, it would be f2_fold
and f2_fold_i
. For the other, it'd be f3_fold
and f3_fold_i
. Etc.
Also, to make the cookie remembers which menu is collapsed and which menu is opened, it also depends on the panelState1
(for 1st menu), panelState2
(for the 2nd), panelState3
, etc
Is there any way to make this shorter and more flexible? So I don't have to add different variable
If you wrap each menu in a div with a class of 'menuWrapper' (or something similar) and then used jQuery's 'each' function, you could execute a function on each of your menus. Change your 'xf_fold' and 'xf_fold_i' ids to 'f_fold' and 'f_fold_i' classes. Then, in the function that runs on each menu item, you can select them based on class. Something like this fiddle could do the trick.
Update
Here's a new fiddle demonstrating a cookie based selected item remembering solution.
After a year, though not achieving exactly what I meant, I found that using the collapsible jQuery plugin is more practical.
精彩评论