摘要提示
“十萬個為什么” 之
為什么在新浪微博上面的輸入文本框有記憶功能?
1.在網頁關閉之前,如果文本框不為空,則將內容保存起來(保存到哪里去==>cookie)
2.在網頁加載的時候,想辦法還原
視頻地址
http://www.tudou.com/programs/view/DOkXpZQnnFw/
示例代碼
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplicationSample.Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <style type="text/css"> #txtInput { height: 113px; width: 694px; } </style> <script src="Scripts/jquery-1.4.4.min.js" type="text/javascript"></script> <script src="Scripts/jquery.cookie.js" type="text/javascript"></script> <script type="text/javascript"> function SaveText() { var date = new Date(); date.setTime(date.getTime() + (30 * 60 * 1000)); $.cookie("input", $("#txtInput").text(), { expires: date }); } $(function () { $("#txtInput").text($.cookie("input")); }); </script> </head> <body onbeforeunload="SaveText()"> <form id="form1" runat="server"> <div> <textarea id="txtInput" rows="5"></textarea> </div> </form> </body> </html>