yaofang123 发表于 2011-12-21 08:44

jquery插件之jQuery操作Cookie的插件使用方法

<span class="Apple-style-span" style="font-family: Arial; font-size: 12px; line-height: 20px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; "><span style="line-height: normal; color: rgb(0, 0, 0); ">jQuery.cookie </span><span style="line-height: normal; color: rgb(0, 0, 0); ">=</span><span style="line-height: normal; color: rgb(0, 0, 0); "> </span><span style="line-height: normal; color: rgb(0, 0, 255); ">function</span><span style="line-height: normal; color: rgb(0, 0, 0); ">(name, value, options) {<br style="line-height: normal; ">    </span><span style="line-height: normal; color: rgb(0, 0, 255); ">if</span><span style="line-height: normal; color: rgb(0, 0, 0); "> (</span><span style="line-height: normal; color: rgb(0, 0, 255); ">typeof</span><span style="line-height: normal; color: rgb(0, 0, 0); "> value </span><span style="line-height: normal; color: rgb(0, 0, 0); ">!=</span><span style="line-height: normal; color: rgb(0, 0, 0); "> </span><span style="line-height: normal; color: rgb(0, 0, 0); ">'</span><span style="line-height: normal; color: rgb(0, 0, 0); ">undefined</span><span style="line-height: normal; color: rgb(0, 0, 0); ">'</span><span style="line-height: normal; color: rgb(0, 0, 0); ">) { </span><span style="line-height: normal; color: rgb(0, 128, 0); ">//</span><span style="line-height: normal; color: rgb(0, 128, 0); "> name and value given, set cookie</span><span style="line-height: normal; color: rgb(0, 128, 0); "><br style="line-height: normal; "></span><span style="line-height: normal; color: rgb(0, 0, 0); ">      options </span><span style="line-height: normal; color: rgb(0, 0, 0); ">=</span><span style="line-height: normal; color: rgb(0, 0, 0); "> options </span><span style="line-height: normal; color: rgb(0, 0, 0); ">||</span><span style="line-height: normal; color: rgb(0, 0, 0); "> {};<br style="line-height: normal; ">      </span><span style="line-height: normal; color: rgb(0, 0, 255); ">if</span><span style="line-height: normal; color: rgb(0, 0, 0); "> (value </span><span style="line-height: normal; color: rgb(0, 0, 0); ">===</span><span style="line-height: normal; color: rgb(0, 0, 0); "> </span><span style="line-height: normal; color: rgb(0, 0, 255); ">null</span><span style="line-height: normal; color: rgb(0, 0, 0); ">) {<br style="line-height: normal; ">            value </span><span style="line-height: normal; color: rgb(0, 0, 0); ">=</span><span style="line-height: normal; color: rgb(0, 0, 0); "> </span><span style="line-height: normal; color: rgb(0, 0, 0); ">''</span><span style="line-height: normal; color: rgb(0, 0, 0); ">;<br style="line-height: normal; ">            options </span><span style="line-height: normal; color: rgb(0, 0, 0); ">=</span><span style="line-height: normal; color: rgb(0, 0, 0); "> $.extend({}, options); </span><span style="line-height: normal; color: rgb(0, 128, 0); ">//</span><span style="line-height: normal; color: rgb(0, 128, 0); "> clone object since it's unexpected behavior if the expired property were changed</span><span style="line-height: normal; color: rgb(0, 128, 0); "><br style="line-height: normal; "></span><span style="line-height: normal; color: rgb(0, 0, 0); ">            options.expires </span><span style="line-height: normal; color: rgb(0, 0, 0); ">=</span><span style="line-height: normal; color: rgb(0, 0, 0); "> </span><span style="line-height: normal; color: rgb(0, 0, 0); ">-</span><span style="line-height: normal; color: rgb(0, 0, 0); ">1</span><span style="line-height: normal; color: rgb(0, 0, 0); ">;<br style="line-height: normal; ">      }<br style="line-height: normal; ">      </span><span style="line-height: normal; color: rgb(0, 0, 255); ">var</span><span style="line-height: normal; color: rgb(0, 0, 0); "> expires </span><span style="line-height: normal; color: rgb(0, 0, 0); ">=</span><span style="line-height: normal; color: rgb(0, 0, 0); "> </span><span style="line-height: normal; color: rgb(0, 0, 0); ">''</span><span style="line-height: normal; color: rgb(0, 0, 0); ">;<br style="line-height: normal; ">      </span><span style="line-height: normal; color: rgb(0, 0, 255); ">if</span><span style="line-height: normal; color: rgb(0, 0, 0); "> (options.expires </span><span style="line-height: normal; color: rgb(0, 0, 0); ">&amp;&amp;</span><span style="line-height: normal; color: rgb(0, 0, 0); "> (</span><span style="line-height: normal; color: rgb(0, 0, 255); ">typeof</span><span style="line-height: normal; color: rgb(0, 0, 0); "> options.expires </span><span style="line-height: normal; color: rgb(0, 0, 0); ">==</span><span style="line-height: normal; color: rgb(0, 0, 0); "> </span><span style="line-height: normal; color: rgb(0, 0, 0); ">'</span><span style="line-height: normal; color: rgb(0, 0, 0); ">number</span><span style="line-height: normal; color: rgb(0, 0, 0); ">'</span><span style="line-height: normal; color: rgb(0, 0, 0); "> </span><span style="line-height: normal; color: rgb(0, 0, 0); ">||</span><span style="line-height: normal; color: rgb(0, 0, 0); "> options.expires.toUTCString)) {<br style="line-height: normal; ">            </span><span style="line-height: normal; color: rgb(0, 0, 255); ">var</span><span style="line-height: normal; color: rgb(0, 0, 0); "> date;<br style="line-height: normal; ">            </span><span style="line-height: normal; color: rgb(0, 0, 255); ">if</span><span style="line-height: normal; color: rgb(0, 0, 0); "> (</span><span style="line-height: normal; color: rgb(0, 0, 255); ">typeof</span><span style="line-height: normal; color: rgb(0, 0, 0); "> options.expires </span><span style="line-height: normal; color: rgb(0, 0, 0); ">==</span><span style="line-height: normal; color: rgb(0, 0, 0); "> </span><span style="line-height: normal; color: rgb(0, 0, 0); ">'</span><span style="line-height: normal; color: rgb(0, 0, 0); ">number</span><span style="line-height: normal; color: rgb(0, 0, 0); ">'</span><span style="line-height: normal; color: rgb(0, 0, 0); ">) {<br style="line-height: normal; ">                date </span><span style="line-height: normal; color: rgb(0, 0, 0); ">=</span><span style="line-height: normal; color: rgb(0, 0, 0); "> </span><span style="line-height: normal; color: rgb(0, 0, 255); ">new</span><span style="line-height: normal; color: rgb(0, 0, 0); "> Date();<br style="line-height: normal; ">                date.setTime(date.getTime()   (options.expires </span><span style="line-height: normal; color: rgb(0, 0, 0); ">*</span><span style="line-height: normal; color: rgb(0, 0, 0); "> </span><span style="line-height: normal; color: rgb(0, 0, 0); ">24</span><span style="line-height: normal; color: rgb(0, 0, 0); "> </span><span style="line-height: normal; color: rgb(0, 0, 0); ">*</span><span style="line-height: normal; color: rgb(0, 0, 0); "> </span><span style="line-height: normal; color: rgb(0, 0, 0); ">60</span><span style="line-height: normal; color: rgb(0, 0, 0); "> </span><span style="line-height: normal; color: rgb(0, 0, 0); ">*</span><span style="line-height: normal; color: rgb(0, 0, 0); "> </span><span style="line-height: normal; color: rgb(0, 0, 0); ">60</span><span style="line-height: normal; color: rgb(0, 0, 0); "> </span><span style="line-height: normal; color: rgb(0, 0, 0); ">*</span><span style="line-height: normal; color: rgb(0, 0, 0); "> </span><span style="line-height: normal; color: rgb(0, 0, 0); ">1000</span><span style="line-height: normal; color: rgb(0, 0, 0); ">));<br style="line-height: normal; ">            } </span><span style="line-height: normal; color: rgb(0, 0, 255); ">else</span><span style="line-height: normal; color: rgb(0, 0, 0); "> {<br style="line-height: normal; ">                date </span><span style="line-height: normal; color: rgb(0, 0, 0); ">=</span><span style="line-height: normal; color: rgb(0, 0, 0); "> options.expires;<br style="line-height: normal; ">            }<br style="line-height: normal; ">            expires </span><span style="line-height: normal; color: rgb(0, 0, 0); ">=</span><span style="line-height: normal; color: rgb(0, 0, 0); "> </span><span style="line-height: normal; color: rgb(0, 0, 0); ">'</span><span style="line-height: normal; color: rgb(0, 0, 0); ">; expires=</span><span style="line-height: normal; color: rgb(0, 0, 0); ">'</span><span style="line-height: normal; color: rgb(0, 0, 0); ">   date.toUTCString(); </span><span style="line-height: normal; color: rgb(0, 128, 0); ">//</span><span style="line-height: normal; color: rgb(0, 128, 0); "> use expires attribute, max-age is not supported by IE</span><span style="line-height: normal; color: rgb(0, 128, 0); "><br style="line-height: normal; "></span><span style="line-height: normal; color: rgb(0, 0, 0); ">      }<br style="line-height: normal; ">      </span><span style="line-height: normal; color: rgb(0, 128, 0); ">//</span><span style="line-height: normal; color: rgb(0, 128, 0); "> NOTE Needed to parenthesize options.path and options.domain</span><span style="line-height: normal; color: rgb(0, 128, 0); "><br style="line-height: normal; "></span><span style="line-height: normal; color: rgb(0, 0, 0); ">      </span><span style="line-height: normal; color: rgb(0, 128, 0); ">//</span><span style="line-height: normal; color: rgb(0, 128, 0); "> in the following expressions, otherwise they evaluate to undefined</span><span style="line-height: normal; color: rgb(0, 128, 0); "><br style="line-height: normal; "></span><span style="line-height: normal; color: rgb(0, 0, 0); ">      </span><span style="line-height: normal; color: rgb(0, 128, 0); ">//</span><span style="line-height: normal; color: rgb(0, 128, 0); "> in the packed version for some reason...</span><span style="line-height: normal; color: rgb(0, 128, 0); "><br style="line-height: normal; "></span><span style="line-height: normal; color: rgb(0, 0, 0); ">      </span><span style="line-height: normal; color: rgb(0, 0, 255); ">var</span><span style="line-height: normal; color: rgb(0, 0, 0); "> path </span><span style="line-height: normal; color: rgb(0, 0, 0); ">=</span><span style="line-height: normal; color: rgb(0, 0, 0); "> options.path </span><span style="line-height: normal; color: rgb(0, 0, 0); ">?</span><span style="line-height: normal; color: rgb(0, 0, 0); "> </span><span style="line-height: normal; color: rgb(0, 0, 0); ">'</span><span style="line-height: normal; color: rgb(0, 0, 0); ">; path=</span><span style="line-height: normal; color: rgb(0, 0, 0); ">'</span><span style="line-height: normal; color: rgb(0, 0, 0); ">   (options.path) : </span><span style="line-height: normal; color: rgb(0, 0, 0); ">''</span><span style="line-height: normal; color: rgb(0, 0, 0); ">;<br style="line-height: normal; ">      </span><span style="line-height: normal; color: rgb(0, 0, 255); ">var</span><span style="line-height: normal; color: rgb(0, 0, 0); "> domain </span><span style="line-height: normal; color: rgb(0, 0, 0); ">=</span><span style="line-height: normal; color: rgb(0, 0, 0); "> options.domain </span><span style="line-height: normal; color: rgb(0, 0, 0); ">?</span><span style="line-height: normal; color: rgb(0, 0, 0); "> </span><span style="line-height: normal; color: rgb(0, 0, 0); ">'</span><span style="line-height: normal; color: rgb(0, 0, 0); ">; domain=</span><span style="line-height: normal; color: rgb(0, 0, 0); ">'</span><span style="line-height: normal; color: rgb(0, 0, 0); ">   (options.domain) : </span><span style="line-height: normal; color: rgb(0, 0, 0); ">''</span><span style="line-height: normal; color: rgb(0, 0, 0); ">;<br style="line-height: normal; ">      </span><span style="line-height: normal; color: rgb(0, 0, 255); ">var</span><span style="line-height: normal; color: rgb(0, 0, 0); "> secure </span><span style="line-height: normal; color: rgb(0, 0, 0); ">=</span><span style="line-height: normal; color: rgb(0, 0, 0); "> options.secure </span><span style="line-height: normal; color: rgb(0, 0, 0); ">?</span><span style="line-height: normal; color: rgb(0, 0, 0); "> </span><span style="line-height: normal; color: rgb(0, 0, 0); ">'</span><span style="line-height: normal; color: rgb(0, 0, 0); ">; secure</span><span style="line-height: normal; color: rgb(0, 0, 0); ">'</span><span style="line-height: normal; color: rgb(0, 0, 0); "> : </span><span style="line-height: normal; color: rgb(0, 0, 0); ">''</span><span style="line-height: normal; color: rgb(0, 0, 0); ">;<br style="line-height: normal; ">      document.cookie </span><span style="line-height: normal; color: rgb(0, 0, 0); ">=</span><span style="line-height: normal; color: rgb(0, 0, 0); "> .join(</span><span style="line-height: normal; color: rgb(0, 0, 0); ">''</span><span style="line-height: normal; color: rgb(0, 0, 0); ">);<br style="line-height: normal; ">    } </span><span style="line-height: normal; color: rgb(0, 0, 255); ">else</span><span style="line-height: normal; color: rgb(0, 0, 0); "> { </span><span style="line-height: normal; color: rgb(0, 128, 0); ">//</span><span style="line-height: normal; color: rgb(0, 128, 0); "> only name given, get cookie</span><span style="line-height: normal; color: rgb(0, 128, 0); "><br style="line-height: normal; "></span><span style="line-height: normal; color: rgb(0, 0, 0); ">      </span><span style="line-height: normal; color: rgb(0, 0, 255); ">var</span><span style="line-height: normal; color: rgb(0, 0, 0); "> cookieValue </span><span style="line-height: normal; color: rgb(0, 0, 0); ">=</span><span style="line-height: normal; color: rgb(0, 0, 0); "> </span><span style="line-height: normal; color: rgb(0, 0, 255); ">null</span><span style="line-height: normal; color: rgb(0, 0, 0); ">;<br style="line-height: normal; ">      </span><span style="line-height: normal; color: rgb(0, 0, 255); ">if</span><span style="line-height: normal; color: rgb(0, 0, 0); "> (document.cookie </span><span style="line-height: normal; color: rgb(0, 0, 0); ">&amp;&amp;</span><span style="line-height: normal; color: rgb(0, 0, 0); "> document.cookie </span><span style="line-height: normal; color: rgb(0, 0, 0); ">!=</span><span style="line-height: normal; color: rgb(0, 0, 0); "> </span><span style="line-height: normal; color: rgb(0, 0, 0); ">''</span><span style="line-height: normal; color: rgb(0, 0, 0); ">) {<br style="line-height: normal; ">            </span><span style="line-height: normal; color: rgb(0, 0, 255); ">var</span><span style="line-height: normal; color: rgb(0, 0, 0); "> cookies </span><span style="line-height: normal; color: rgb(0, 0, 0); ">=</span><span style="line-height: normal; color: rgb(0, 0, 0); "> document.cookie.split(</span><span style="line-height: normal; color: rgb(0, 0, 0); ">'</span><span style="line-height: normal; color: rgb(0, 0, 0); ">;</span><span style="line-height: normal; color: rgb(0, 0, 0); ">'</span><span style="line-height: normal; color: rgb(0, 0, 0); ">);<br style="line-height: normal; ">            </span><span style="line-height: normal; color: rgb(0, 0, 255); ">for</span><span style="line-height: normal; color: rgb(0, 0, 0); "> (</span><span style="line-height: normal; color: rgb(0, 0, 255); ">var</span><span style="line-height: normal; color: rgb(0, 0, 0); "> i </span><span style="line-height: normal; color: rgb(0, 0, 0); ">=</span><span style="line-height: normal; color: rgb(0, 0, 0); "> </span><span style="line-height: normal; color: rgb(0, 0, 0); ">0</span><span style="line-height: normal; color: rgb(0, 0, 0); ">; i </span><span style="line-height: normal; color: rgb(0, 0, 0); ">&lt;</span><span style="line-height: normal; color: rgb(0, 0, 0); "> cookies.length; i) {<br style="line-height: normal; ">                </span><span style="line-height: normal; color: rgb(0, 0, 255); ">var</span><span style="line-height: normal; color: rgb(0, 0, 0); "> cookie </span><span style="line-height: normal; color: rgb(0, 0, 0); ">=</span><span style="line-height: normal; color: rgb(0, 0, 0); "> jQuery.trim(cookies);<br style="line-height: normal; ">                </span><span style="line-height: normal; color: rgb(0, 128, 0); ">//</span><span style="line-height: normal; color: rgb(0, 128, 0); "> Does this cookie string begin with the name we want?</span><span style="line-height: normal; color: rgb(0, 128, 0); "><br style="line-height: normal; "></span><span style="line-height: normal; color: rgb(0, 0, 0); ">                </span><span style="line-height: normal; color: rgb(0, 0, 255); ">if</span><span style="line-height: normal; color: rgb(0, 0, 0); "> (cookie.substring(</span><span style="line-height: normal; color: rgb(0, 0, 0); ">0</span><span style="line-height: normal; color: rgb(0, 0, 0); ">, name.length   </span><span style="line-height: normal; color: rgb(0, 0, 0); ">1</span><span style="line-height: normal; color: rgb(0, 0, 0); ">) </span><span style="line-height: normal; color: rgb(0, 0, 0); ">==</span><span style="line-height: normal; color: rgb(0, 0, 0); "> (name   </span><span style="line-height: normal; color: rgb(0, 0, 0); ">'</span><span style="line-height: normal; color: rgb(0, 0, 0); ">=</span><span style="line-height: normal; color: rgb(0, 0, 0); ">'</span><span style="line-height: normal; color: rgb(0, 0, 0); ">)) {<br style="line-height: normal; ">                  cookieValue </span><span style="line-height: normal; color: rgb(0, 0, 0); ">=</span><span style="line-height: normal; color: rgb(0, 0, 0); "> decodeURIComponent(cookie.substring(name.length   </span><span style="line-height: normal; color: rgb(0, 0, 0); ">1</span><span style="line-height: normal; color: rgb(0, 0, 0); ">));<br style="line-height: normal; ">                  </span><span style="line-height: normal; color: rgb(0, 0, 255); ">break</span><span style="line-height: normal; color: rgb(0, 0, 0); ">;<br style="line-height: normal; ">                }<br style="line-height: normal; ">            }<br style="line-height: normal; ">      }<br style="line-height: normal; ">      </span><span style="line-height: normal; color: rgb(0, 0, 255); ">return</span><span style="line-height: normal; color: rgb(0, 0, 0); "> cookieValue;<br style="line-height: normal; ">    }<br style="line-height: normal; "><br style="line-height: normal; ">};<br style="line-height: normal; "><br style="line-height: normal; ">使用方法<br style="line-height: normal; "><br style="line-height: normal; "></span><span style="line-height: normal; color: rgb(0, 0, 0); ">jQuery操作cookie的插件,大概的使用方法如下<br style="line-height: normal; ">$.cookie(</span><span style="line-height: normal; color: rgb(0, 0, 0); ">'</span><span style="line-height: normal; color: rgb(0, 0, 0); ">the_cookie</span><span style="line-height: normal; color: rgb(0, 0, 0); ">'</span><span style="line-height: normal; color: rgb(0, 0, 0); ">); </span><span style="line-height: normal; color: rgb(0, 128, 0); ">//</span><span style="line-height: normal; color: rgb(0, 128, 0); ">读取Cookie值</span><span style="line-height: normal; color: rgb(0, 128, 0); "><br style="line-height: normal; "></span><span style="line-height: normal; color: rgb(0, 0, 0); ">$.cookie(</span><span style="line-height: normal; color: rgb(0, 0, 0); ">'</span><span style="line-height: normal; color: rgb(0, 0, 0); ">the_cookie</span><span style="line-height: normal; color: rgb(0, 0, 0); ">'</span><span style="line-height: normal; color: rgb(0, 0, 0); ">, </span><span style="line-height: normal; color: rgb(0, 0, 0); ">'</span><span style="line-height: normal; color: rgb(0, 0, 0); ">the_value</span><span style="line-height: normal; color: rgb(0, 0, 0); ">'</span><span style="line-height: normal; color: rgb(0, 0, 0); ">); </span><span style="line-height: normal; color: rgb(0, 128, 0); ">//</span><span style="line-height: normal; color: rgb(0, 128, 0); ">设置cookie的值</span><span style="line-height: normal; color: rgb(0, 128, 0); "><br style="line-height: normal; "></span><span style="line-height: normal; color: rgb(0, 0, 0); ">$.cookie(</span><span style="line-height: normal; color: rgb(0, 0, 0); ">'</span><span style="line-height: normal; color: rgb(0, 0, 0); ">the_cookie</span><span style="line-height: normal; color: rgb(0, 0, 0); ">'</span><span style="line-height: normal; color: rgb(0, 0, 0); ">, </span><span style="line-height: normal; color: rgb(0, 0, 0); ">'</span><span style="line-height: normal; color: rgb(0, 0, 0); ">the_value</span><span style="line-height: normal; color: rgb(0, 0, 0); ">'</span><span style="line-height: normal; color: rgb(0, 0, 0); ">, {expires: </span><span style="line-height: normal; color: rgb(0, 0, 0); ">7</span><span style="line-height: normal; color: rgb(0, 0, 0); ">, path: </span><span style="line-height: normal; color: rgb(0, 0, 0); ">'</span><span style="line-height: normal; color: rgb(0, 0, 0); ">/</span><span style="line-height: normal; color: rgb(0, 0, 0); ">'</span><span style="line-height: normal; color: rgb(0, 0, 0); ">, domain: </span><span style="line-height: normal; color: rgb(0, 0, 0); ">'</span><span style="line-height: normal; color: rgb(0, 0, 0); ">jquery.com</span><span style="line-height: normal; color: rgb(0, 0, 0); ">'</span><span style="line-height: normal; color: rgb(0, 0, 0); ">, secure: </span><span style="line-height: normal; color: rgb(0, 0, 255); ">true</span><span style="line-height: normal; color: rgb(0, 0, 0); ">});</span><span style="line-height: normal; color: rgb(0, 128, 0); ">//</span><span style="line-height: normal; color: rgb(0, 128, 0); ">新建一个cookie 包括有效期 路径 域名等</span><span style="line-height: normal; color: rgb(0, 128, 0); "><br style="line-height: normal; "></span><span style="line-height: normal; color: rgb(0, 0, 0); ">$.cookie(</span><span style="line-height: normal; color: rgb(0, 0, 0); ">'</span><span style="line-height: normal; color: rgb(0, 0, 0); ">the_cookie</span><span style="line-height: normal; color: rgb(0, 0, 0); ">'</span><span style="line-height: normal; color: rgb(0, 0, 0); ">, </span><span style="line-height: normal; color: rgb(0, 0, 0); ">'</span><span style="line-height: normal; color: rgb(0, 0, 0); ">the_value</span><span style="line-height: normal; color: rgb(0, 0, 0); ">'</span><span style="line-height: normal; color: rgb(0, 0, 0); ">); </span><span style="line-height: normal; color: rgb(0, 128, 0); ">//</span><span style="line-height: normal; color: rgb(0, 128, 0); ">新建cookie</span><span style="line-height: normal; color: rgb(0, 128, 0); "><br style="line-height: normal; "></span><span style="line-height: normal; color: rgb(0, 0, 0); ">$.cookie(</span><span style="line-height: normal; color: rgb(0, 0, 0); ">'</span><span style="line-height: normal; color: rgb(0, 0, 0); ">the_cookie</span><span style="line-height: normal; color: rgb(0, 0, 0); ">'</span><span style="line-height: normal; color: rgb(0, 0, 0); ">, </span><span style="line-height: normal; color: rgb(0, 0, 255); ">null</span><span style="line-height: normal; color: rgb(0, 0, 0); ">); </span><span style="line-height: normal; color: rgb(0, 128, 0); ">//</span><span style="line-height: normal; color: rgb(0, 128, 0); ">删除一个cookie<br style="line-height: normal; "><br style="line-height: normal; "><br style="line-height: normal; "></span><p style="line-height: normal; ">设置一个名称为blog,值为css9.net的cookie:</p><p style="line-height: normal; ">$.cookie("blog", "css9.net");</p><p style="line-height: normal; ">设置一个名称为blog,值为css9.net的cookie,同时设置过期时间(expires属性)为7天:</p><p style="line-height: normal; ">$.cookie("blog", "css9.net", { expires: 7 });</p><p style="line-height: normal; ">设置一个名称为blog,值为css9.net的cookie,设置过期时间(expires属性)为7天,同时设置cookie的path属性为”/admin”</p><p style="line-height: normal; ">$.cookie("blog", "css9.net", { path: '/admin', expires: 7 });</p><p style="line-height: normal; ">读取Cookie:</p><p style="line-height: normal; ">读取名称为blog的cookie值:</p><p style="line-height: normal; ">alert( $.cookie("blog") );</p><p style="line-height: normal; ">删除cookie:</p>$.cookie("example", null);</span>
页: [1]
查看完整版本: jquery插件之jQuery操作Cookie的插件使用方法