JQuery + ajax implementation.

Source: Internet
Author: User

Demo page implementation Copy codeThe Code is as follows: <div class = "digg" id = "digg">
<Div class = "good"> <a href = "#">
<P> This document is good. </p>
<Div class = "bar">
<Div id = "g_img" style = "width: 70%"> </div>
</Div>
<Span class = "num" id = "num"> 70% (7000) </span> </a> </div>
<Div class = "bad"> <a href = "#">
<P> documents to be improved </p>
<Div class = "bar">
<Div id = "B _img" style = "width: 30%"> </div>
</Div>
<Span class = "num"> 30% (3000) </span> </a> </div>
</Div>

The main point is to control the g_img width by percentage. css code will not be pasted out.
DEMO code:Copy codeThe Code is as follows: <! 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>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> Digg </title>
<Style type = "text/css">
*{
Padding: 0;
Margin: 0;
}
. Digg {
Height: auto;
Width: pixel PX;
Font-size: 12px;
Font-weight: normal;
}
. Digg {
Display: block;
Height: 48px;
Width: 189px;
Background-image: url (images/mark.gif );
Background-repeat: no-repeat;
Position: relative;
Color: #000;
Text-decoration: none;
}
. Digg. good {
Margin-bottom: 10px;
Margin-top: 5px;
}
. Digg. good {
Background-position:-189px 0px;
}
. Digg. good a: hover {
Background-position: 0px 0px;
}
. Digg. bad {
Background-position:-pixel PX 0px;
}
. Digg. bad a: hover {
Background-position:-567px 0px;
}
. Digg a p {
Padding-left: 30px;
Line-height: 25px;
}
. Digg. bar {
Background-color: white;
Height: 5px;
Left: 20px;
Overflow: hidden;
Position: absolute;
Text-align: left;
Top: 30px;
Width: 55px;
}
. Bar # g_img {
Background-image: url (images/sprites.gif );
Background-repeat: repeat-x;
Height: 5px;
Width: auto;
}
. Bar # B _img {
Background-image: url (images/sprites.gif );
Background-repeat: repeat-x;
Height: 5px;
Width: auto;
Background-position: 0px-5px;
}
. Num {
Color: #333;
Font: normal 100 10px/12px Tahoma;
Left: 80px;
Position: absolute;
Top: 26px;
}
. Digg. good. bar {
Border: 1px solid #40A300;
}
. Digg. bad. bar {
Border: 1px solid #555;
}
</Style>
</Head>
<Body>
<Div class = "digg" id = "digg">
<Div class = "good"> <a href = "#">
<P> This document is good. </p>
<Div class = "bar">
<Div id = "g_img" style = "width: 70%"> </div>
</Div>
<Span class = "num" id = "num"> 70% (7000) </span> </a> </div>
<Div class = "bad"> <a href = "#">
<P> documents to be improved </p>
<Div class = "bar">
<Div id = "B _img" style = "width: 30%"> </div>
</Div>
<Span class = "num"> 30% (3000) </span> </a> </div>
</Div>
</Body>
</Html>

With the demo, other implementations are much more convenient. First, the page gets html. The page is loaded for the first time, and the background data is obtained using ajax instead of being directly displayed. (Asp is used as the background language for convenience of testing)
The following is the html code output by asp.Copy codeThe Code is as follows: function getdigshtml () 'outputs html
Dim rsajax, SQL, str, digsnum, undigsnum, digsnumall, digsper, undigsper
Set rsajax = server. CreateObject ("adodb. recordset ")
SQL = "select * from dig where id = 1"
Rsajax. open SQL, conn, 1, 1
Digsnum = rsajax ("digs ")
Undigsnum = rsajax ("undigs ")
If isnull (digsnum) then digsnum = 0
If isnull (undigsnum) then undigsnum = 0
Digsnumdigsnumall = digsnum + undigsnum
If digsnumall = 0 then
Digsper = 0
Undigsper = 0
Else
Digsper = FormatNumber (cint (digsnum)/cint (digsnumall), 3) * 100
Undigsper = FormatNumber (cint (undigsnum)/cint (digsnumall), (3) * 100
End if
Str = "<div class = 'good'>"
Strstr = str & "<a href = JavaScript: isdigs ('digs ')>"
Strstr = str & "<p> This document is good </p> <div class = 'bar'> <div id = 'G _ img 'style = 'width: "& digsper &" % '> </div>"
Strstr = str & "<span class = 'num'>" & digsper & "% (" & digsnum & ") </span>"
Strstr = str & "</a> </div> <div class = 'bad'>"
Strstr = str & "<a href = JavaScript: isdigs ('undigs ')>"
Strstr = str & "<p> document to be improved </p> <div class = 'bar'> <div id = 'B _ img' style = 'width: "& undigsper &" % '> </div>"
Strstr = str & "<span class = 'num'>" & undigsper & "% (" & undigsnum & ") </span>"
Strstr = str & "</a> </div>"
Getdigshtml = str
End function

After the output is complete, we will get the result from the front end. At this time, we will use jquery ajax. Why not use ajax directly? The reason is very simple. I won't ..... Let's take a look at the ajax code in jquery, which is very simple.Copy codeThe Code is as follows: function getdigshtml () // obtain the top level and step on the html
{
$. Ajax ({
Type: 'post ',
Url: 'digg. asp ',
Data: 'Action = getdigshtml ',
Success: function (msg ){
$ ("# Digg" cmd.html (msg );
}
})
}

The output is complete, and the next step is the digs and undigs operations, which is similar to getting html code.Copy codeThe Code is as follows: function isdigs (digtype) // top it down and step on it.
{
$. Ajax ({
Type: 'post ',
Url: 'digg. asp ',
Data: 'Action = digs & digtype = '+ digtype,
/* BeforeSend: function (){
$ ("# Vote"). hide ();
$ ("# Loadings"). show ();
}. The ajax request displays the loading effect */
Success: function (msg ){
Switch (msg)
{
/* The backend is used to determine
Case '1 ':
$ ("# Loadings"). hide ();
$ ("# Vote"). show ();
Alert ("Please log on first! ");
Break;
Case '2 ':
$ ("# Loadings"). hide ();
$ ("# Vote"). show ();
Alert ("Download it first before proceeding! ");
Break;
Case '4 ':
$ ("# Loadings"). hide ();
$ ("# Vote"). show ();
Alert ("You have participated in the evaluation! ");
Break ;*/
Case '3 ':
Getdigshtml (); // rebind html
// $ ("# Loadings"). hide ();
// $ ("# Vote"). show ();
Alert ("Thank you for your participation! ");
Break;
Default:
}
}
})
}

Commented out code: part of the code is used for legal verification of Background Data. The beforeSend method is related to the operations before the ajax request is executed (for loading more)
The last step is to re-obtain and bind the getdigshtml () after each data submission and return a successful result, thus ensuring the real-time data.
The Demo code requires an asp environment. You can test it.
Packaging: http://www.jb51.net/jiaoben/28489.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.