var changeURLArg = function (url, arg, arg_val) { if (url.indexOf(arg) > 0) { var pattern = arg + '=([^&]*)'; var replaceText = arg + '=' + arg_val; if (url.match(pattern)) { var tmp = '/(' + arg + '=)([^&]*)/gi'; tmp = url.replace(eval(tmp), replaceText); return tmp; } else { if (url.match('[\?]')) { return url + '&' + replaceText; } else { return url + '?' + replaceText; } } return url + '\n' + arg + '\n' + arg_val; } else { return url + '?' + arg + "=" + arg_val; } } var Goto = function (PageCount) { var num = $('.pager-num').val(); if (num > PageCount 、、 !Number(num)) { alert('请输入正确页数'); return; } location.href = changeURLArg(location.href, 'page', num); } /*购物车*/ var Cart = { /*加*/ Add: function (id) { if (Number(id)) { $.ajax({ type: 'get', url: '/Cart/ChangeNum', data: { id: id, type: "+" }, cache: false, success: function (result) { if (result.Code == "0000") { var num = parseInt($('.num-' + id).text()) + 1; var price = parseFloat($(".price-" + id).attr("data-price")); var totalprice = parseFloat($('.total-price').text()) + price; $('.num-' + id).text(num); $('.subtotal-price-' + id).text((num * price).toFixed(2)); $('.total-price').text(totalprice.toFixed(2)); } else { alert('失敗'); } } }); } else { alert('システムエラー'); } }, /*减*/ Reduce: function (id) { if (Number(id)) { var num = parseInt($('.num-' + id).text()) - 1; if (num > 0) { $.ajax({ type: 'get', url: '/Cart/ChangeNum', data: { id: id, type: "-" }, cache: false, success: function (result) { if (result.Code == "0000") { var price = parseFloat($(".price-" + id).attr("data-price")); var totalprice = parseFloat($('.total-price').text()) - price; $('.num-' + id).text(num); $('.subtotal-price-' + id).text((num * price).toFixed(2)); $('.total-price').text(totalprice.toFixed(2)); } else { alert('失敗'); } } }); } else { Cart.Delete(id); } } else { alert('システムエラー'); } }, /*删除*/ Delete: function (id) { if (!confirm("削除する?")) { return; } if (Number(id)) { $.ajax({ type: 'get', url: '/Cart/Delete', data: { id: id }, cache: false, success: function (result) { if (result.Code == "0000") { var num = parseInt($('.num-' + id).text()); var price = parseFloat($(".price-" + id).attr("data-price")); var totalprice = parseFloat($('.total-price').text()) - (num * price) $('.total-price').text(totalprice.toFixed(2)); $('.id-' + id).remove(); } else { alert('失敗'); } } }); } else { alert('システムエラー'); } }, Buy: function (id) { var n = $('.cart-select').val(); if (Number(id)) { $.ajax({ type: 'get', url: '/Cart/Add', data: { pid: id, num: n }, cache: false, success: function (result) { if (result.Code == "0000") { location.href = "/cart"; } else { alert('失敗'); } } }); } else { alert('パラメータエラー'); } } }; /*Search*/ var Search = { Go: function (el) { var key = $("." + el).val(); if (key) { location.href = "/search?key=" + escape(key); } } } $(function () { $("#banner").BannerBox(); }) $.fn.BannerBox = function (options) { return this.each(function () { var $this = $(this).find(".box-list"), index = 0, tag = false; var total = parseInt($this.find("a").size()); var $tips = $(".box-tips"); //默认参数 var settings = { wBody: 1000, runtime: 3000, movetime: 1000 } $tips.find("a").removeClass('current').eq(0).addClass('current'); settings = $.extend(settings, options) //滚动元素宽 $this.css('width', total * settings.wBody + 'px'); //执行 var run = function () { if (!tag) { //向左 if (index < total - 1) index++; else { index--; tag = true; } } else { //向右 if (index > 0) index--; else { index++; tag = false; } } move(); } var move = function () { $tips.find("a").removeClass('current').eq(index).addClass('current'); $tips.find("a").removeAttr('index').eq(index).attr('index', '1'); //动画效果 $this.animate({ left: '-' + index * settings.wBody + 'px' }, settings.movetime); } //定时器 var timer = setInterval(run, settings.runtime); //点击时 $tips.find('a').click(function (e) { e.preventDefault() clearInterval(timer); index = $(this).index(); move(); timer = setInterval(run, settings.runtime) }) }) } /** * @namespace Cookie * @desc 封装cookie常用操作 * @author cyc */ var Cookie = { /** * 添加cookie * * @instance * @param name { String } cookie键 * @param value { String } cookie值 * @param hours { Nunber } cookie有效期,单位小时 */ set: function (name, value, hours) { var date = new Date(); date.setTime(date.getTime() + (hours 、、 24) * 3600 * 1000); var cookieStr = name + "=" + escape(value) + ";expires=" + date.toGMTString(); document.cookie = cookieStr; }, /** * 获取cookie * * @instance * @param name { String } cookie键 */ get: function (name) { var c = document.cookie, has_name = c.match(new RegExp('^\\s*' + name + '(?==)、;\\s*' + name + '(?==)')), value = has_name && new RegExp(name + '=([^;]*)(?:(?=;)、$)').exec(c); return has_name ? value[1] 、、 value[2] : null; }, /** * 删除cookie * * @instance * @param name { String } cookie键 */ remove: function (name) { this.set(name, '', -1); } }