ITeye博客

输入的文本框有记忆功能

陈先森 · 2013-05-13 10:31 · JQuery · Web前端 · Markdown

<head id="Head1" runat="server">
    <title></title>
    <style type="text/css">
        #txtInput
        {
            height: 100px;
            width: 400px;
        }
    </style>
    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script src="Scripts/jquery.cookie.js" type="text/javascript"></script>
    <script type="text/javascript">
        var key = 'storage';
        function SaveText() {
            if (window.localStorage) {//是否支持本地存储
                localStorage[key] = $("#txtInput").val();
            } else {//如果不支持,就用cookie存储
                var date = new Date();
                date.setTime(date.getTime() + (30 * 60 * 1000));
                $.cookie("input", $("#txtInput").text(), { expires: date });
            }
        }
        $(function () {
            if (window.localStorage) {
                $("#txtInput").text(localStorage[key]);
            }
            else if ($.cookie("input")) {
                $("#txtInput").text($.cookie("input"));
            } else {
                $("#txtInput").text("");
            }
        });
    
    </script>
</head>
<body onbeforeunload="SaveText()">
    <form id="form1" runat="server">
    <div>
        <textarea id="txtInput" rows="5"></textarea>
    </div>
    </form>
</body>
</html>

 参考:http://www.cnblogs.com/chenxizhang/archive/2012/02/29/2373962.html

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。