1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <form action="http://www.baidu.com" id="frm"> 用户名<input type="text" name="" id=""> 密码<input type="password" name="" id=""> <input type="submit" value="登陆"> <a href="javascript:;" id="clear">清除记录</a> </form>
<script> function setCookie (name, value, iDay){ var oDate = new Date(); oDate.setDate(oDate.getDate()+iDay); document.cookie = name+"="+value+";expires="+oDate; }
function getCookie (name){ var cookieArr = document.cookie.split("; ");
for (var i = 0; i < cookieArr.length; i++) { var oneCookie = cookieArr[i].split("="); if (oneCookie[0] === name) { return oneCookie[1]; } }
return ''; }
function removeCookie (name) { setCookie(name, '1', -1); }
function addListener (target, type, handler) { if (target.attachEventListener) { target.attachEventListener(type, handler, false); } else if (target.attachEvent) { target.attachEvent("on"+type, handler); } else { target["on"+type] = handler; } }
window.onload = function (){ var oForm = document.getElementById("frm"); var oUser = oForm.getElementsByTagName("input")[0]; var oClear = oForm.getElementsByTagName("a")[0]; oForm.onsubmit = function (){ var username = oUser.value; setCookie("username", username, 30); }
oUser.value = getCookie('username');
addListener(oClear, "click", function(){ removeCookie("username"); oUser.value = ''; }); } </script> </body> </html>
|