大家好,我有这段代码....
<html>
<head>
<script class="jsbin" src="http://ajax.googleapis.com /ajax/libs/jquery/1/jquery.min.js"></script>
<script>
$(function(){
$(".mytableCol3").click(function(){
$(this).addClass("on").parent().siblings("tr").find("td").removeClass("on");
});
});
</script>
<style>
.on { background-color:red; color:#ffffff; }
</style>
</head>
<body>
<table class="mytable" border=1>
<tbody>
<tr>
<td class="mytableCol3"><a href="google.com">google</a></td>
</tr>
<tr>
<td class="mytableCol3"><a href="yahoo.com">yahoo</a></td>
</tr>
<tr>
<td class="mytableCol3"><a href="bing.com">bing</a></td>
</tr>
</tbody>
</table>
<table class="mytable" border=1>
</body>
</html>
上面的代码通过在单元格之间切换红色来正常工作,并且当点击它时它还会将页面重定向到“特定位置”。请检查 demo ,您可以首先观察到单元格变为红色然后它将被重定向到 google/yahoo/bing ,但是现在当他们通过单击后退按钮/(代码我写的代码)返回时我需要做什么,特定的单元格是选中的内容仍应以红色突出显示....我在 session 中确实这样做了但不完全是..任何人都可以帮助我解决这个问题....
最佳答案
你可以用 $.cookie 做到这一点
为每一行分配一个 ID 并设置 cookie,然后检查该 cookie 是否有可用的 id:
<table class="mytable" border=1>
<tbody>
<tr>
<td class="mytableCol3" id="google"><a href="http://google.com">google</a></td>
</tr>
<tr>
<td class="mytableCol3" id="yahoo"><a href="http://yahoo.com">yahoo</a></td>
</tr>
<tr>
<td class="mytableCol3" id="bing"><a href="http://bing.com">bing</a></td>
</tr>
</tbody>
</table>
和javascript:
$(function(){
$(".mytableCol3").click(function(){
$(this).addClass("on").parent().siblings("tr").find("td").removeClass("on");
$.cookie('clicked', $(this).attr('id'));
});
if($('#'+$.cookie('clicked'))){
$('#'+$.cookie('clicked')).addClass('on');
}
});
关于php - 在后退按钮后切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12352210/