---
title: "输入的文本框有记忆功能"
author: "陈先森"
date: "2013-05-13T10:31:56Z"
tags: []
categories: ["Web前端"]
slug: "untitled"
draft: false
lastmod: "2013-05-13T10:31:56Z"
id: 1867737
url: "https://zhanchan.iteye.com/blog/1867737"
---

```
<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](http://www.cnblogs.com/chenxizhang/archive/2012/02/29/2373962.html)