接口包含:添加收藏,刪除收藏,檢測收藏php
前臺傳值:id與收藏頁來源類型 css
獲取網頁數據ID值方法2種:app
1:後臺傳值放隱藏域中函數
2:用JS方法取網址IDpost
function getUrlParam(){ return location.href.match(/([^?=&]+)(=([^&]*))/g).reduce((a, v) => ((a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1)), a), {}); }
調用:this
id=getUrlParam().school_id;
實現收藏代碼:url
//收藏 var delete_id , type = $('#collectionType').val(), id=$('#newsId').val(); //這裏的兩個函數分別代碼fn1,fn2 check_collection( function(){ $('.collection').addClass('active'); console.log(1) } , function(){ $('.collection').removeClass('active'); console.log(2) } ); //檢查是否收藏 function check_collection( fn1 , fn2){ $.post(com.url.check_is_collection , { collection_type:type, collection_id:id } , function(res){ if(res.result == 'succ'){ delete_id = res.data.id; fn1();//調到接口說明收藏初始時加active上方的check_collection函數,不然不加 }else { fn2(); } }) } //添加收藏 , 刪除收藏 function collection(url , newsId , fn){ $.post(url , { collection_type:type, collection_id:newsId } , function(res){ if(res.result == 'succ'){ fn() console.log(fn);//執行下方添加或取消收藏函數 }else { com.prompt(res.message); } }) } $('.collection').click(function(){ var _this = $(this); var source = com.get_source(); if((window.location.href).indexOf('passport') < 0){ //未登陸 if(is_login <= 0 && !$('#login_ifr').attr('src')){ var ifHtml = '<iframe id="login_ifr" style="display:block;" class="login_ifr none" src="'+passportHost + '/xuemaotouch/login/login_layer.php?back_url='+location.href+'&source='+source+'&source_platform='+source_platform+'"></iframe>'; $('body').append(ifHtml); $("body").css("overflow","hidden"); return false; } if($('#login_ifr').attr('src')){ $('#login_ifr').show(); $("body").css("overflow","hidden"); return false; } //增長自定義屬性,給數埋點用 if(_this.hasClass('active')){ _this.attr('status' , 'cancel'); }else { _this.attr('status' , 'add'); } clearTimeout(timer); timer = setTimeout(function(){ check_collection(function(){ collection(com.url.delete_collection , delete_id , function(){ $('.collection').removeClass('active'); com.prompt('取消成功'); }); },function(){ collection(com.url.add_collection , id , function(){ $('.collection').addClass('active'); com.prompt('收藏成功'); }); } ); } , 1000) } });