我正在嘗試創建用戶界面,但是當我刷新頁面時,它將從導航鏈接中刪除類current
。刷新頁面當前類
我下面這個教程http://www.ssdtutorials.com/tutorials/series/list-grid-view-jquery-cookies.html
下面是我的代碼:
$(function() {
var cc = $.cookie('list_grid');
if (cc == 'g') {
$('#products').addClass('grid');
} else {
$('#products').removeClass('grid');
}
});
$(document).ready(function() {
$('#grid').click(function() {
$(".current").removeClass("current");
$(this).addClass("current");
$('#products').fadeOut(500, function() {
$(this).addClass('grid').fadeIn(500);
$.cookie('list_grid', 'g');
});
return false;
});
$('#list').click(function() {
$(".current").removeClass("current");
$(this).addClass("current");
$('#products').fadeOut(500, function() {
$(this).removeClass('grid').fadeIn(500);
$.cookie('list_grid', null);
});
return false;
});
});
如果有人需要這樣的HTML代碼
<div id="wrapper">
<div id="navigation">
<a href="#" id="list">List view</a>
<a href="#" id="grid">Grid view</a>
</div>
<div id="products">
<?php for($i = 1; $i <= 4; $i++) { ?>
<div class="product">
<div class="wrapper">
<div class="image">
<img src="images/0<?php echo $i; ?>.jpg" alt="image <?php echo $i; ?>" width="160" height="160" />
</div>
<div class="description">
<h1><a href="#">Article title</a></h1>
<p>Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt.</p>
</div><!-- end description -->
</div><!-- end wrapper -->
</div><!-- end product -->
<?php } ?>
</div><!-- end products -->
</div><!-- end wrapper -->
看起來像你的代碼只添加'grid'類,取決於cookie而不是'current'? – 2012-01-29 19:30:42
您的初始html標記是否包含開頭的類? – Kristian 2012-01-29 19:30:46