- <html>
- <head>
- <title>JS+Cookiesm</title>
- <script type="text/javascript" language="javascript" src="http://tg.yescar.cn/js/jquery-1.4.2.min.js" ></script>
- </head>
- <body>
- <script>
- //cookie相关函数
- // -----------------------------------------------------------
- var CookieCars = {
- setCookie: function(name, value) {
- expiryday = new Date();
- expiryday.setTime(expiryday.getTime() + 30 * 30 * 24 * 60 * 60 * 1 * 1000);
- document.cookie = name + "=" + escape(value) + "; expires=" + expiryday.toGMTString();
- },
- getCookie: function(name) {
- var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
- if (arr != null) {
- return unescape(arr[2]);
- }
- return null;
- },
- clearCookie: function(name) {
- var exp = new Date();
- exp.setTime(exp.getTime() - 1);
- var cval = CookieCars.getCookie(name);
- if (cval != null) document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
- }
- };
- //-------------------------------------------------------------------
- function addCarToYescar(id, name) {
- var Yescar = CookieCars.getCookie("ActiveNewYescar");
- var com_arr = null;
- if (Yescar) {
- com_arr = Yescar.split("|");
- if (com_arr.length >= 10) {
- alert("对比车型不能多于10个");
- return;
- }
- if (Yescar.indexOf("id" + id + ",") >= 0) {
- alert("您选择的车型,已经在对比列表中!");
- return;
- }
- }
- else {
- com_arr = new Array();
- }
- com_arr.push('id' + id + ',' + name);
- CookieCars.clearCookie("ActiveNewYescar");
- CookieCars.setCookie("ActiveNewYescar", com_arr.join("|"));
- showCarsLi() ;
- }
- function showCarsLi() {
- var Yescar = CookieCars.getCookie("ActiveNewYescar");
- //if (!WaitYescarObj.IDListULObj) {
- // if (document.getElementById('idListULForWaitYescar'))
- // { WaitYescarObj.IDListULObj = document.getElementById('idListULForWaitYescar'); }
- // else
- // { return; }
- //}
- if (Yescar) {
- $("#div1").html('');
- var com_arr = Yescar.split("|");
- var tempHTML = new Array();
- for (var i = 0; i < com_arr.length; i++) {
- var id = com_arr[i].split(",")[0].substring(2, com_arr[i].split(",")[0].length);
- var name = com_arr[i].split(",")[1];
- //startCarCsInfoRequestForWaitYescar(id);
- //if (WaitYescarObj.CsName != "" && WaitYescarObj.CsAllSpell != "") {
- // tempHTML.push("<li><a href=\"http://car.bitauto.com/" + WaitYescarObj.CsAllSpell + "/m" + id + "/\">" + WaitYescarObj.CsName + " " + name + "</a><div class=\"bnt_YescarClose\" onclick=\"javascript:delYescar('" + id + "','" + name.replace("'", "’") + "');\">删除</div></li>");
- //}
- //else {
- tempHTML.push("<li><a href=\"\">" + name + "</a><a href=\"javascript:void('0')\" onclick=\"javascript:delYescar('" + id + "','" + name.replace("'", "’") + "');\">删除</a></li>");
- //}
- // 综述页已添加的变更
- $("#div1").html(tempHTML.join(""));
- }
- }
- }
- function delYescar(id, name) {
- var Yescar = CookieCars.getCookie("ActiveNewYescar");
- com_new_arr = new Array();
- if (Yescar) {
- var com_arr = Yescar.split("|");
- for (var i = 0; i < com_arr.length; i++) {
- if (com_arr[i].indexOf("id" + id + ",") < 0) {
- com_new_arr.push(com_arr[i]);
- }
- }
- }
- CookieCars.clearCookie("ActiveNewYescar");
- CookieCars.setCookie("ActiveNewYescar", com_new_arr.join("|"));
- showCarsLi();
- }
- $(document).ready(function(){
- showCarsLi();
- });
- function AddSelect()
- {
- //alert(obj.id);
- //alert($("#"+obj.id+"").val())
- //alert($("#idSelect").text());
- // var item = $("select[@name=items] option[@selected]").text()
- // alert($("#idSelect option:selected").text());
- addCarToYescar($("#idSelect").val(),$("#idSelect option:selected").text());
- }
- function Clear()
- {
- CookieCars.clearCookie("ActiveNewYescar");
- showCarsLi();
- }
- </script>
- <div id="div1">
- </div>
- <div>
- <a href="javascript:void(0)" onclick="addCarToYescar('123423','宝马11320')" target="_bank" >werwer</a>
- <a href="javascript:void(0)" onclick="addCarToYescar('134123','黑马222王子')" target="_bank" >werwer</a>
- <a href="javascript:void(0)" onclick="addCarToYescar('5223','天22朝')" target="_bank" >werwer</a>
- <a href="javascript:void(0)" onclick="addCarToYescar('4122','米22娜')" target="_bank" >werwer</a>
- <select id="idSelect">
- <option value="112">天朝帝都</option>
- <option value="1212">黑色流金</option>
- </select>
- <select id="idSelect2">
- <option value="3457">晓风残月</option>
- <option value="1212">飞龙在天</option>
- </select>
- <button type="button" onclick="AddSelect()" class="button">添加</button>
- <input type="button" onclick="Clear()" value="清除"></input>
- </div>
- </body >
- </html>
JS 操作Cookies 存取数组形式
最新推荐文章于 2023-07-19 17:42:18 发布