const sciCompare = { data: { id: '', classid: '', list: [] }, init(id,classid){ this.data.id = id; this.data.classid = classid; this.getList(); $('head').append(` `); this.render(); this.event(); setInterval(()=>{ this.render(); },3000); }, getList(){ const cache = localStorage.getItem('sci.compare'); this.data.list = cache ? cache.split(',').map(e=>Number(e)) : []; }, event(){ $(document).on('click','.add-compare',()=>{ this.toggle() }); }, render(){ $('.add-compare').html(this.isExist() ? '-移除对比' : '+加入对比'); $('.compare-float').remove(); this.data.list.length > 0 && $('body').append('对比'); }, isExist(){ this.getList(); return this.data.list.indexOf(this.data.id)>=0 }, toggle(){ if(!this.isExist()){ if(this.data.list.length>=4){ alert('最多4个对比'); window.location.href = "/sci/compare.html"; return ; } this.data.list.push(this.data.id); }else{ const index = this.data.list.findIndex(e=>e==this.data.id); this.data.list.splice(index,1); } localStorage.setItem('sci.compare', this.data.list.join(',')); this.render(); } } ;if(window.$){ const infoId = 11017; const infoClassId = 125; const compareList = (localStorage.getItem('sci.compare') || '').split(','); $(function(){ sciCompare.init(infoId); }); }else{ console.error('模块需要依赖jquery'); }