-
Notifications
You must be signed in to change notification settings - Fork 27
/
compare.js
199 lines (191 loc) · 5.45 KB
/
compare.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* $Id: compare.js 15469 2008-12-19 06:34:44Z testyang $ */
var Compare = new Object();
Compare = {
add : function(goodsId, goodsName, type, goodsImg, price)
{
var count = 0;
for (var k in this.data)
{
if (typeof(this.data[k]) == "function")
continue;
if (this.data[k].t != type) {
alert(goods_type_different.replace("%s", goodsName));
return;
}
count++;
}
if (this.data[goodsId])
{
this.remove(goodsId);
return;
}
else
{
this.data[goodsId] = {n:goodsName,t:type,img:goodsImg,p:price};
$('.compare-btn').each(function(index, element) {
if($(this).attr('data-goods') == goodsId)
$(this).css('background-position', '0 -99px');
});
}
if(count>=4)
{
alert('对比商品已有4个,请删除再行添加。');
return;
}
if($('#compareBox').css('display') == 'none')
{
$('#compareBox').css('display', 'block');
$('.compareHolder').css('display', 'none');
}
this.save();
this.init();
},
init : function(){
this.data = new Object();
var cookieValue = document.getCookie("compareItems");
if (cookieValue != null) {
eval("this.data = " + cookieValue);
}
this.compareBox = document.getElementById(this.compareBoxId);
this.compareList = document.getElementById(this.compareListId);
this.compareList.id = "compareList";
this.compareList.innerHTML = "";
var self = this;
var count = 0;
for (var key in this.data)
{
if(typeof(this.data[key]) == "function")
continue;
var img = document.createElement('IMG');
var aimg = document.createElement('A');
var dl = document.createElement("DL");
var span = document.createElement("SPAN");
var a = document.createElement('A');
var dt = document.createElement('DT');
var dd = document.createElement('DD');
var span = document.createElement('SPAN');
var strong = document.createElement('STRONG');
img.style.width = "50px";
img.style.height = "50px";
img.src = this.data[key].img;
aimg.href = 'goods.php?id='+key;
aimg.appendChild(img);
span.style.overflow = "hidden";
span.style.width = "100px";
span.style.height = "20px";
span.style.display = "block";
a.innerHTML = this.data[key].n;
a.className = 'name';
a.href = 'goods.php?id='+key;
span.appendChild(a);
dt.appendChild(aimg);
dd.appendChild(a);
dl.appendChild(dt);
dl.appendChild(dd);
strong.innerHTML = this.data[key].p;
var delBtn = document.createElement("A");
delBtn.innerHTML = "删除";
delBtn.className = key;
delBtn.onclick = function(){
self.remove(this.className);
}
dl.onmousemove = function (){
this.childNodes[1].childNodes[1].childNodes[1].style.visibility = 'visible';
};
dl.onmouseout = function (){
this.childNodes[1].childNodes[1].childNodes[1].style.visibility = 'hidden';
};
span.appendChild(strong);
span.appendChild(delBtn);
dd.appendChild(span);
this.compareList.appendChild(dl);
count++;
}
while(count<4)
{
var dl = document.createElement("DL");
var dt = document.createElement('DT');
var dd = document.createElement('DD');
dt.style.backgroundColor = '#F6F6F6';
dt.innerHTML = count+1;
dd.innerHTML = '您还可以继续添加';
dl.appendChild(dt);
dl.appendChild(dd);
this.compareList.appendChild(dl);
count++;
}
var operate = document.createElement('DIV');
var comp = document.createElement('A');
var clr = document.createElement('A');
//'<div class="diff-operate"><a class="btn-compare-b compare-active" href="#none" id="goto-contrast" target="_blank">对比</a><a class="del-items">清空对比栏</a></div>';
comp.innerHTML = '对比';
comp.className = 'compare';
comp.onclick = function() {
var cookieValue = document.getCookie("compareItems");
var obj = JSON.parse(cookieValue);
var url = document.location.href;
url = url.substring(0,url.lastIndexOf('/')+1) + "compare.php";
var i = 0;
for(var k in obj)
{
if(typeof(obj[k])=="function")
continue;
if(i==0)
url += "?goods[]=" + k;
else
url += "&goods[]=" + k;
i++;
}
if(i<2)
{
alert(compare_no_goods);
return ;
}
document.location.href = url;
}
clr.innerHTML = '清空对比栏';
clr.className = 'clear';
clr.onclick = function(){
Compare.clear();
};
operate.className = 'operate';
operate.appendChild(comp);
operate.appendChild(clr);
compareList.appendChild(operate);
},
save : function()
{
var date = new Date();
date.setTime(date.getTime() + 99999999);
document.setCookie("compareItems", JSON.stringify(this.data));
},
clear : function()
{
for(var k in this.data)
{
delete this.data[k];
}
$('.compare-btn').each(function(index, element) {
$(this).css('background-position', '0 -75px');
});
this.save();
this.init();
},
remove : function(id){
delete this.data[id];
$('.compare-btn').each(function(index, element) {
if($(this).attr('data-goods') == id)
$(this).css('background-position', '0 -75px');
});
if($('#compareBox').css('display') == 'none')
{
$('#compareBox').css('display', 'block');
$('.compareHolder').css('display', 'none');
}
this.save();
this.init();
},
show : 1,
compareBoxId : 'compareBox',
compareListId : 'compareList'
}