使用 Twitter Bootstrap,你如何“记住"?用户操作? [英] Using Twitter Bootstrap, how can you "remember" user actions?

查看:22
本文介绍了使用 Twitter Bootstrap,你如何“记住"?用户操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Bootstrap Alerts 框,并且希望这些框能够记住"它们是否已关闭.这样当用户登录会员区并关闭警报时;下次他们访问该网站时,警报仍然消失.

有没有办法做到这一点?

<a class="close" data-dismiss="alert" href="#">&times;</a>当我点击 &times;我希望它不再出现.</div>

解决方案

您可能必须将该首选项存储在 cookie 或服务器本身中.另一个 SO 线程.

用于存储 cookie基本上,您需要做的是围绕您的代码实现 javascript.为简单起见,我使用了 jQuery 和一个 jQuery cookie 插件.

//jQuery 从 Google CDN 中拉取<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>//jQuery cookie 插件.您必须自行下载:https://github.com/carhartl/jquery-cookie<script src="/path/to/jquery.cookie.js"></script>//这里的 jQuery 动作<脚本>函数 runOnLoad(){if($.cookie('alert-box') == null) {$.cookie('alert-box', 'open', { expires: 7 });} else if($.cookie('alert-box') == 'close') {$(".close").hide();}//这里的 php 伪代码(如果您使用的是服务器选项)<?phpif(检查数据库中的隐藏选项== true){echo '$(".close").hide();}?>}函数隐藏我(){$.cookie('alert-box', 'close', {expires:7 });$(".close").hide();}<body onload="runOnLoad()"><div class="alert-message success" data-alert="alert"><a class="close" data-dismiss="alert" href="hideMe.php" onclick="hideMe()" >&times;</a>当我点击 &times;我希望这个永远不要再出现.

如果您使用服务器选项,则必须将 hideMe.php 编码为:

  1. 在数据库的表中设置隐藏选项,即将 userPreference 设置为 true
  2. 将用户重定向回他正在查看的页面.

免责声明:这些代码旨在让您了解如何完成.但是,无法保证它会按原样工作,因为我没有测试代码.

注意事项:

  1. 我使用了 jQuery 的 hide.阅读它.
  2. 您可以在此处阅读有关 jQuery cookie 插件的更多信息.

I am using the Bootstrap Alerts boxes, and would like these boxes to "remember" if they have been closed. This way when a user logs in to the members area and closes an alert; the next time they visit the site the alert is still gone.

Is there any way to do this?

<div class="alert-message success" data-alert="alert">
<a class="close" data-dismiss="alert" href="#">&times;</a>When I click &times; I'd like this to never appear again.</div>

解决方案

You probably will have to store that preference in the cookies or on the server itself. A good read can be found in another SO thread.

For storing cookies Basically, what you have to do is to implement javascript around your codes. For simplicity, I utilise jQuery and a jQuery cookie plugin.

// jQuery pulled from Google CDN
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
// jQuery cookie plugin. You will have to download it on your own: https://github.com/carhartl/jquery-cookie
<script src="/path/to/jquery.cookie.js"></script>
// jQuery action here
<script>
    function runOnLoad(){
        if($.cookie('alert-box') == null) {
            $.cookie('alert-box', 'open', { expires: 7 });
        } else if($.cookie('alert-box') == 'close') {
            $(".close").hide();
        }

        // php psuedo code here (if you are using the server option)
        <?php
            if(check database for hide option == true){
                echo '$(".close").hide();
            }
        ?>
    }
    function hideMe(){
        $.cookie('alert-box', 'close', {expires:7 });
        $(".close").hide();
    }
</script>

<body onload="runOnLoad()">
    <div class="alert-message success" data-alert="alert">
        <a class="close" data-dismiss="alert" href="hideMe.php" onclick="hideMe()" >&times;</a>When I click &times; I'd like this to never appear again.
    </div>
</body>

If you are using the server option, you have to code hideMe.php to:

  1. set the hide option in a table in your database, i.e. userPreference to true
  2. redirect the user back to the page that he is viewing.

Disclaimer: These codes are to give you an idea how it can be done. However, there is no gaurantee that it will work as it is, as I did not test the code.

Notes:

  1. I utilised jQuery's hide. Read on it.
  2. You can read more about jQuery cookie plugin here.

这篇关于使用 Twitter Bootstrap,你如何“记住"?用户操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆