这是一个Cookie数据生成的列表,
每次单击查询会存储一个域名,并把最后一次查询的域名放在最上方。本例子最多存储10个,大家可以根据自己情况进行设置
下在咱们一起来看看是怎么实现的吧、
先写一个操作Cookie的JS文件如下
Code
代码如下 | 复制代码 |
function getid(id) { return (typeof id == 'string') ? document.getElementById(id) : id }; function getOffsetTop(el, p) { var _t = el.offsetTop; while (el = el.offsetParent) { if (el == p) break; _t += el.offsetTop } return _t }; function getOffsetLeft(el, p) { var _l = el.offsetLeft; while (el = el.offsetParent) { if (el == p) break; _l += el.offsetLeft } return _l }; var currentInput = null; function BoxShow(e) { var input = e; if (!input.id) { input = e.target ? e.target : e.srcElement; } currentInput = input; FillUrls("site"); var box = getid("allSitesBoxHdl"); if (box.style.display == 'block' && currentInput.id == input.id) { return; } box.style.left = (getOffsetLeft(input)) + 'px'; box.style.top = (getOffsetTop(input) + (input.offsetHeight - 1)) + 'px'; box.style.width = (input.offsetWidth - 4) + 'px'; box.style.display = 'block'; } function BoxShowUrls(e) { BoxShow(e); } function InputSetValue(val) { var obj = currentInput; obj.value = val; if (obj.getAttribute('url') == 'true') { var tags = document.getElementsByTagName('input'); for (var i = 0; i < tags.length; i++) { if (tags[i].getAttribute('url') == 'true' && tags[i] != obj) { tags[i].value = val; } } } BoxHide(); } //删除时使用,传入一个要删除的值就可以删除 function DelAllSitesValue(value) { var allSites = $.cookie("site"); allSites = allSites.replace(value + "|", ""); $.cookie("site", allSites, { expires: 7 }); FillUrls("site"); } function BoxHide() { if (getid("allSitesBoxHdl")) { getid("allSitesBoxHdl").style.display = 'none'; } } //加载列表 function FillUrls(cookieName) { var urls = $.cookie(cookieName); var html = ""; if (urls) { var urllist = urls.split('|'); var forlength = 0; var stringcookie; for (var i = urllist.length - 1; i >= 0; i--) { var textval = urllist[i]; if ($.trim(textval) != "" && $.trim(textval) != "undefined") { html += " "; forlength = forlength + 1; if (forlength > 10) { $.cookie("site", stringcookie, { expires: 7 }); break; } else { stringcookie = textval + "|" + stringcookie; } } } } else { html += " } getid("allSitesBoxContent").innerHTML = html; } function closeIME(e) { var obj = e.target ? e.target : e.srcElement; obj.style.imeMode = 'disabled'; } function OnPaste(e) { var obj = e.target ? e.target : e.srcElement; setTimeout("MoveHttp('" + obj.id + "')", 100); } function MoveHttp(id) { var val = getid(id).value; val = val.replace("http://", ""); if (val[val.length - 1] == '/') { val = val.substring(0, val.length - 1); } getid(id).value = val; } function OnKeyup(e) { var obj = e.target ? e.target : e.srcElement; setTimeout("addInput('" + obj.id + "')", 200); } function addInput(id) { var obj = getid(id); //如果是一个没有True的input不执行 if (obj.getAttribute('url') == 'true') { if (obj.value.indexOf('。') > 0) { obj.value = obj.value.replace('。', '.'); } var tags = document.getElementsByTagName('input'); for (var i = 0; i < tags.length; i++) { if (tags[i].getAttribute('url') == 'true' && tags[i] != obj) { tags[i].value = obj.value; } } } } function Init() { $("#allSitesBoxHdl")[0].style.display = 'none'; $(":text").each(function () { $(this).bind("keyup", OnKeyup); $(this).bind("mousedown", BoxShowUrls); $(this).bind("mouseout", BoxHide); $(this).bind("focus", closeIME); $(this).bind("paste", OnPaste); $(this).bind("mouseout", BoxHide); $(this)[0].setAttribute('autocomplete', 'off'); }); //取出Cookie var icpSite = $.cookie("site"); if (icpSite) { //取出Cookie不为空的话就给当前框 icpSite = icpSite.split('|')[0]; $("#site").val(icpSite); } } |
代码如下 | 复制代码 |
onmouseover="this.style.display='block'" onmouseout="this.style.display='none'"> |
//加载列表
代码如下 | 复制代码 |
function FillUrls(cookieName) { var urls = $.cookie(cookieName); var html = ""; if (urls) { var urllist = urls.split('|'); var forlength = 0; var stringcookie; for (var i = urllist.length - 1; i >= 0; i--) { var textval = urllist[i]; if ($.trim(textval) != "" && $.trim(textval) != "undefined") { html += " "; forlength = forlength + 1; if (forlength > 10) {//在这里我只加载10条,大家可以根据自己的情况进行调整 $.cookie("site", stringcookie, { expires: 7 }); break; } else {//如果超出10个的话就取最后10个 stringcookie = textval + "|" + stringcookie; } } } } else { html += " } getid("allSitesBoxContent").innerHTML = html; } |
//操作Cookie类
代码如下 | 复制代码 |
function setCookie(name, value) { var oldcookie = $.cookie(name); if (oldcookie == null) { $.cookie(name, value, { expires: 7 }); } else { if ($.cookie(name).indexOf(value) == -1) { $.cookie(name, oldcookie + "|" + value, { expires: 7 }); } else { $.cookie(name, oldcookie.replace(value, "") + "|" + value, { expires: 7 }); } } FillUrls(name); } |
代码如下 | 复制代码 |
setCookie("site", strdomin); |
进行具体的测试
$.cookie("domain", value, { expires: 7, domain: "7c.com" });