Digg 頂 踩(jQuery+Ajax)

來源:互聯網
上載者:User
//Default.aspx
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server">    <title>digg</title>    <link href="css/page.css" rel="stylesheet" type="text/css" media="all" />    <script src="js/cookie.js" type="text/javascript" language="javascript"  ></script>    <script src="js/jquery-1.3.2.min.js" type="text/javascript" language="javascript"  ></script>    <script type="text/javascript">  function postDigg(ftype,aid){    //設定cookie  var saveid = GetCookie('diggid');    if(saveid != null)    {        var saveids = saveid.split(',');        var hasid = false;        saveid = '';        j = 1;        for(i=saveids.length-1;i>=0;i--)        {            if(saveids[i]==aid && hasid) continue;            else {                if(saveids[i]==aid && !hasid) hasid = true;                saveid += (saveid=='' ? saveids[i] : ','+saveids[i]);                j++;                if(j==20 && hasid) break;                if(j==19 && !hasid) break;            }        }        if(hasid) { alert("您已經頂過該帖,請不要重複頂帖 !"); return; }        else saveid += ','+aid;        SetCookie('diggid',saveid,1);    }    else    {        SetCookie('diggid',aid,1);    }          //擷取資料      $.ajax({        url:"Digg.aspx?action="+ftype+"&id="+aid+" &time="+new Date().toString(),        type:'GET',        success:function(){            $('#newdigg').html(arguments[0]);        }    });} function  getDigg(aid){    $.ajax({        url:"Digg.aspx?id="+aid+"  & time="+new Date().toString(),        type:'GET',        success:function(){            $('#newdigg').html(arguments[0]);        }    });}  </script>       </head><body>    <form id="form1" runat="server">         <div class="newdigg" id="newdigg">  </div>       <script  language="javascript" type="text/javascript">getDigg(197);</script>       <div class="boxoff">   <strong>------分隔線----------------------------</strong>   </div>          </form></body></html>

 

 

//Digg.aspx

using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;public partial class Digg : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {        string s = Request["id"].ToString();        int aid = int.Parse(s);        if (Request["action"] != null)  //一定要這個        {            string action = Request["action"].ToString();            db d = new db();            if (action == "good")            {                d.excuteSQL("Update Digg set goodpost=goodpost+1,lastpost=getdate() where contentid=" + aid);            }            else if (action == "bad")            {                d.excuteSQL("Update Digg set badpost=badpost+1,lastpost=getdate() where contentid=" + aid);            }        }        string ss = getDigg(aid);        Response.Write(ss);           }    public string  getDigg(int strId)    {        int goodper;        int badper;        string digg = "";        db d = new db();        DataTable dt = d.getDT("Select goodpost,badpost  From Digg  where contentid="+strId);        foreach (DataRow dr in dt.Rows)        {            int goodposts = Convert.ToInt32(dr["goodpost"]);            int badposts = Convert.ToInt32(dr["badpost"]);            if (goodposts + badposts == 0)            {                goodper = badper = 0;            }            else            {                int alls = goodposts + badposts;                goodper = Convert.ToInt32((double)goodposts / alls * 100);                badper = 100 - goodper;            }            digg += "<div class=\"diggbox digg_good\" onmousemove=\"this.style.backgroundPosition='left bottom';\"  onmouseout=\"this.style.backgroundPosition='left top';\" onclick=\"postDigg('good'," + strId + ")\">";            digg += "<div class=\"digg_act\">頂一下</div>";            digg += "<div class=\"digg_num\">(" + goodposts + ")</div>";            digg += "<div class=\"digg_percent\">";            digg += "<div class=\"digg_percent_bar\"><span style=\"width:" + goodper + "%\"></span></div>";            digg += "<div class=\"digg_percent_num\">" + goodper + "%</div>";            digg += "</div>";            digg += "</div>";            digg += "<div class=\"diggbox digg_bad\" onmousemove=\"this.style.backgroundPosition='right bottom';\"  onmouseout=\"this.style.backgroundPosition='right top';\" onclick=\"postDigg('bad'," + strId + ")\">";            digg += "<div class=\"digg_act\">踩一下</div>";            digg += "<div class=\"digg_num\">(" + badposts + ")</div>";            digg += "<div class=\"digg_percent\">";            digg += "<div class=\"digg_percent_bar\"><span style=\"width:" + badper + "%\"></span></div>";            digg += "<div class=\"digg_percent_num\">" + badper + "%</div>";            digg += "</div>";            digg += "</div>";                  }              return digg;          }}

 

//cookie.js

//讀寫cookie函數function GetCookie(c_name){    if (document.cookie.length > 0)    {        c_start = document.cookie.indexOf(c_name + "=")        if (c_start != -1)        {            c_start = c_start + c_name.length + 1;            c_end   = document.cookie.indexOf(";",c_start);            if (c_end == -1)            {                c_end = document.cookie.length;            }            return unescape(document.cookie.substring(c_start,c_end));        }    }    return null}function SetCookie(c_name,value,expiredays){    var exdate = new Date();    exdate.setDate(exdate.getDate() + expiredays);    document.cookie = c_name + "=" +escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());     //使設定的有效時間正確。增加toGMTString()}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.