首页 > 代码库 > jquery weibo 留言

jquery weibo 留言

  1 <script>  2 function getCnTime(s){  3     var oDate = new Date(s*1000);  4     return oDate.getFullYear()+-+toDou((oDate.getMonth()+1))+-+toDou(oDate.getDate())+ +toDou(oDate.getHours())+:+toDou(oDate.getMinutes())+:+toDou(oDate.getSeconds());   5 }  6 function toDou(n){  7     return n<10?0+n:‘‘+n;  8 }  9 $(function(){ 10     var oSendBtn=$(#sendBtn); 11     var oTjText=$(#tjText); 12     var oMessList=$(#messList); 13     var url=weibo.php; 14     var iNow = 1; 15     var oN=$(.noContent); 16     //创建留言 17     oSendBtn.on(click,function(){ 18         $.ajax({ 19             url:url, 20             data:{ 21                 act:add, 22                 content:oTjText.val() 23             }, 24             cache:false, 25             success:function(str){ 26                 var json = eval((+str+)); 27                 if(!json.error){ 28                      var oDiv=createReply(json.id,oTjText.val(),0,0,json.time); 29                         if(oMessList.children()){ 30                             oMessList.prepend($(oDiv)); 31                         }else{ 32                             $(oDiv).appendTo(oMessList); 33                         } 34                         if(oMessList.children().length>6){ 35                             oMessList.children().eq(oMessList.children().length-1).remove(); 36                         } 37                         get_page_count(); 38                 } 39                  40                 oTjText.val(‘‘); 41                  42             }, 43  44             error:function(){ 45                 alert(no); 46             } 47         }); 48     }); 49     //获取某页数据 50     function getPage(n){ 51         $.ajax({ 52             url:url, 53             data:{ 54                 act:get, 55                 page:n 56             }, 57             cache:false, 58             success:function(str){ 59                 var arr = eval((+str+)); 60                 oMessList.html(‘‘); 61                 $.each(arr,function(index){ 62                     var oDiv = createReply(arr[index].id,arr[index].content,arr[index].acc,arr[index].ref,arr[index].time); 63                     $(oDiv).appendTo(oMessList); 64                 }) 65             }, 66             error:function(){ 67                 alert(no); 68             } 69         }); 70             if(n>0){ 71                 oN.css({display:none}); 72             }else{ 73                 oN.css({display:block}); 74             } 75     } 76     //获取页数 77     var oPage=$(#page); 78     function get_page_count(){ 79         $.ajax({ 80             url:url, 81             data:{ 82                 act:get_page_count 83             }, 84             cache:false, 85             success:function(str){ 86                 oPage.html(‘‘); 87                 var json = eval((+str+)); 88                 for(var i =0;i<json.count;i++){ 89                     var oA = document.createElement(a); 90                     oA.innerHTML = i+1; 91                     oA.href = http://www.mamicode.com/javascript:;; 92                     oPage[0].appendChild(oA); 93                     if(i == iNow-1){ 94                         oA.className = active; 95                     } 96                     $(oA).on(click,function(){ 97                     $.each(oPage.children(),function(index){ 98                         oPage.children().eq(index).removeClass(active); 99                     });100                     $(this).addClass(active);101                     iNow=$(this).html();102                     getPage(iNow);103                     });104                 }105             },106             error:function(){107                 alert(no);108             }109         });110     }111     getPage(1);112     //创建一条留言113     function createReply(id,content,acc,ref,time){114         var oDiv=document.createElement(div);115         $(oDiv).addClass(reply);116         var str=117         <p class="replyContent">+content+</p>+118         <p class="operation">+119         <span class="replyTime">+getCnTime(time)+</span>+120         <span class="handle">+121         <a href="javascript:;" class="top">+acc+</a>+122         <a href="javascript:;" class="down_icon">+ref+</a>+123         <a href="javascript:;" class="cut">删除</a>+124         </span>+125         </p>;126         $(oDiv).html(str);127         //删除128         var oDel=$(oDiv).children().eq(1).children().eq(1).children().eq(2);129         oDel.on(click,function(){130             $.ajax({131                 url:url,132                 data:{133                     act:del,134                     id:id    135                 },136                 cache:false,137                 success:function(str){138                     var json = eval((+str+));139                     if(!json.error){140                         var boo=window.confirm(确定删除吗?);141                         if(boo){142                             oDiv.remove();143                             get_page_count();144                             getPage(iNow);145                         }146                         147                     }148                 }149             });150         });151         //152         var oAcc=$(oDiv).children().eq(1).children().eq(1).children().eq(0);153         oAcc.on(click,function(){154             if(!localStorage[acc+id]){155                 $.ajax({156                     url:url,157                     data:{158                         act:acc,159                         id:id    160                     },161                     cache:false,162                     success:function(str){163                         var json = eval((+str+));164                         if(!json.error){165                             alert(顶成功);166                             oAcc.html(parseInt(oAcc.html())+1);167                             localStorage[acc+id]=1;168                             // setCookie(‘acc‘+id,1,1);可用cookie做169                         }170                     }171                 });172             }else{173                 alert(一天只能一次);174             }175             176         });177         //178         var oRef=$(oDiv).children().eq(1).children().eq(1).children().eq(1);179         oRef.on(click,function(){180             if(!localStorage[ref+id]){181                 $.ajax({182                     url:url,183                     data:{184                         act:ref,185                         id:id    186                     },187                     cache:false,188                     success:function(str){189                         var json = eval((+str+));190                         if(!json.error){191                             alert(踩成功);192                             oRef.html(parseInt(parseInt(oRef.html())+1));193                             localStorage[ref+id]=1;194                         }195                     }196                 });197             }else{198                 alert(您来过了);199             }200         });201         return oDiv;202     }203     get_page_count();204 });

 

jquery weibo 留言