Jquery操作Cookie|多个输入框同时输入效果|下拉列表经典例子
先看看实现后的效果吧,
这是一个Cookie数据生成的列表,
每次单击查询会存储一个域名,并把最后一次查询的域名放在最上方。本例子最多存储10个,大家可以根据自己情况进行设置
下在咱们一起来看看是怎么实现的吧、
先写一个操作Cookie的JS文件如下
/** * Cookie plugin * * Copyright (c) http://sufei.cnblogs.com */ jQuery.cookie = function (name, value, options) { if (typeof value != 'undefined') { // name and value given, set cookie options = options || {}; if (value === null) { value = ''; options.expires = -1; } var expires = ''; if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) { var date; if (typeof options.expires == 'number') { date = new Date(); date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000)); } else { date = options.expires; } expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE } // CAUTION: Needed to parenthesize options.path and options.domain // in the following expressions, otherwise they evaluate to undefined // in the packed version for some reason... var path = options.path ? '; path=' + (options.path) : ''; var domain = options.domain ? '; domain=' + (options.domain) : ''; var secure = options.secure ? '; secure' : ''; document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join(''); } else { // only name given, get cookie var cookieValue = null; if (document.cookie && document.cookie != '') { var cookies = document.cookie.split(';'); for (var i = 0; i < cookies.length; i++) { var cookie = jQuery.trim(cookies[i]); // Does this cookie string begin with the name we want? if (cookie.substring(0, name.length + 1) == (name + '=')) { cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); break; } } } return cookieValue; } };有了这个文件之后我们来给要使用这Cookie的Text做一下处理吧,
代码如下
function Init() { $("#allSitesBoxHdl")[0].style.display = 'none';//不显示Cookie的列表 $(":text").each(function () {//给所有的text加属性 $(this).bind("keyup", OnKeyup); //按键时 $(this).bind("mousedown", BoxShowUrls); //鼠标安下时 $(this).bind("mouseout", BoxHide); //鼠标离开时 $(this).bind("focus", closeIME); //处到焦点时 $(this).bind("paste", OnPaste); //处理http;// $(this)[0].setAttribute('autocomplete', 'off'); }); //取出当前Cookie var icpSite = $.cookie("site"); if (icpSite) {//如果Cookie存在的话取第一个值给当前框 icpSite = icpSite.split('|')[0]; $("#site").val(icpSite); } }
这里面所用到的方法如下,具体的意思我就不一个一个的解释了,如果需要说明的请大家留言,我一定回复
在这里面还附带了这样一个效果,就是同时输入多个输入框的值,如下图
如果那个输入框要使用这样的效果只要添加一个属性为url="true"就行了,这样方便 可操作性强,想给那个框加效果就加上这个属性,不想加的直接不加url="true"
就OK了。
在使用这个效果的界面添加如下代码
<div style="display: none; position: absolute;" id="allSitesBoxHdl" class="classlist" onmouseover="this.style.display='block'" onmouseout="this.style.display='none'"> <ul id="allSitesBoxContent"> </ul> </div> <script type="text/javascript"> Init();</script>
除此之外的JS直接放在一个Js文件里,引用进来就行了
下拉列表是怎么加载的呢?看下面的一个方法就知道了
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 += "<li class=\"lis\"><a href=\"javascript:InputSetValue('" + textval + "');\">" + textval + "</a></li><br/>"; forlength = forlength + 1; if (forlength > 10) { $.cookie("site", stringcookie, { expires: 7 }); break; } else { stringcookie = textval + "|" + stringcookie; } } } } else { html += "<li>没有记录</li>" } getid("allSitesBoxContent").innerHTML = html; }
完成了这些之后我们只需要在单击查询时进行存储Cookie就行了,看下面的方法
调用 时这样写
setCookie("site", strdomin);
好了功能完成,大家如果想看真实效果的话,进行具体的测试
代码写的不是很好,希望大家多提提建议,我们进行相应修改争取更完善。
Cookie是存储的客户端的,一个并且只能访问同域名下的Cookie,子域名之间可以相互访问,只要加上
domain属性就行了,存储的方法如下
$.cookie("domain", value, { expires: 7, domain: "baidu.com" });
取的时间直接写 $.cookie("domain");就好了,只要是子域名,都这样调用,这样可以达到本域名下的Cookie共享的功能。
Cookie的有效利用会给我们的网站带来N多意想不到的效果和功能,大家交流下。希望多提提建议
欢迎大家转载,如有转载请注明文章来自: http://sufei.cnblogs.com/
签名:做一番一生引以为豪的事业;在有生之年报答帮过我的人;并有能力帮助需要帮助的人;
软件开发,功能定制,请联系我
QQ:361983679 Email:[email protected] MSN:[email protected]