php - 在后退按钮后切换

标签 php javascript jquery html css

大家好,我有这段代码....

<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');
    }
});

你可以为 cookie 使用普通的 javascript,我用过 plugin对于 example .

关于php - 在后退按钮后切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12352210/

相关文章:

php - 使用 Joomla MVC 更新 MySQL 表行,

php - 如果任何作业失败,Cron 作业会自动重试

php - 不调用发布数据

javascript - 使用 Jquery 淡出进行页面过渡

php - Mantis Bug Tracker 发送邮件的日期格式

php - 使用 codeIgniter 查询结果

javascript - 是否可以使用 jQuery/javascript 每隔 x 像素重复放置一个 DIV

javascript - 如何在 JQuery 中使用 ReplaceWith 在 for 循环中追加 <td>

javascript - 循环遍历表以生成 json

javascript - 在页面加载问题上使用 javascript 删除和添加类