Ajax no Refresh dynamic call stock information (improved version) _jquery

Source: Internet
Author: User
Copy Code code as follows:

<title>ajax test</title>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<script type= "Text/javascript" src= "Jquery.js" ></script>
<script type= "Text/javascript" ....
function Ajaxrequest () ... {
var url = $ ("#stockurl"). Val () + $ ("#stockinit"). Val ();
$.ajax (.....) {
Url:url,
Type: ' Get ',
DataType: ' HTML ',
timeout:2000,
Success:function (response) ... {
var stocks = Response.split (';');
for (var i=0; i<stocks.length-1; i++) ... {
var content = Stocks[i];
var temp1 = content.split (' = ') [0];
var temp2 = content.split (' = ') [1];
var code = TEMP1.SUBSTR (temp1.length-6, 6);
var Temp3 = temp2.replace (' "', ');
var name = Temp3.split (', ') [0];
var tday_f = Temp3.split (', ') [1];
var yest_f = Temp3.split (', ') [2];
var curr_f = Temp3.split (', ') [3];
var temp_f = Curr_f-yest_f;

$ (' #a ' +i). HTML (code);
$ (' #b ' +i). HTML (name);
if (Curr_f > Yest_f) ... {
$ (' #c ' +i). HTML ("<font color= ' Red ' >" + curr_f + "</font>");
else if (Curr_f < Yest_f) ... {
$ (' #c ' +i). HTML ("<font color= ' green ' >" + curr_f + "</font>");
else ... {
$ (' #c ' +i). html (curr_f);
}
$ (' #d ' +i). html (Tday_f);
$ (' #e ' +i). html (yest_f);
if (Temp_f > 0) ... {
$ (' #f ' +i). HTML ("<font color= ' Red ' >" + temp_f.tofixed (2) + "</font>");
$ (' #g ' +i). HTML ("<font color= ' Red ' >" + ((temp_f/yest_f) *). ToFixed (2) + "</font>%");
else if (Temp_f < 0) ... {
$ (' #f ' +i). HTML ("<font color= ' green ' >" + temp_f.tofixed (2) + "</font>");
$ (' #g ' +i). HTML ("<font color= ' green ' >" + ((temp_f/yest_f) *). ToFixed (2) + "</font>%");
else ... {
$ (' #f ' +i). HTML (temp_f.tofixed (2));
$ (' #g ' +i). HTML (((temp_f/yest_f) *). ToFixed (2) + "%");
}
$ (' #h ' +i). HTML (Temp3.split (', ') [4]);
$ (' #i ' +i). HTML (Temp3.split (', ') [5]);
}
}
});
}

$ (document). Ready (function () ... {

var stocks = $ ("#stockinit"). Val (). Split (', ');
for (var i=0; i<stocks.length; i++) ... {
Addrows ();
}

$ ("#insrow"). Click (function (). {

var stockcd = $ ("#stockid"). Val ();
if (stockcd = = "") ... {
Alert ("ticker symbol cannot be empty!") ");
$ ("#stockid"). focus ();
Return
}
if (stockcd.length!= 6) ... {
Alert ("Ticker number can only be 6 digits!") ");
$ ("#stockid"). focus ();
Return
}
var div = ' sh ';
if (document.getElementById ("sz_id") ... checked) ... {div = "sz";}
var stocks = $ ("#stockinit"). Val ();
$ ("#stockinit"). Val (Stocks + "," + div + stockcd);
Addrows ();
});

$ ("#delrow"). Click (function (). {
var table = document.getElementById ("TableId");
var index = table.rows.length;
if (Index < 2) ... {
Alert ("There are no rows to delete!");
else ... {
Table.deleterow (index-1);
var StockValue = $ ("#stockinit"). Val ();
var count = Stockvalue.split (', '). length;
if (count = = 1) ... {
StockValue = "";
else ... {
StockValue = stockvalue.substr (0, stockvalue.length-9);
}
$ ("#stockinit"). Val (StockValue);
}
});

Window.setinterval ("Ajaxrequest ()", 3000);
});

function Addrows () ... {
var table = document.getElementById ("TableId");
var index = table.rows.length;
var str= "";
var row = index-1;
str+= ' <tr class= ' tr_cls ' > '
str+= ' <td align= ' center ' ><span id= ' + ' a ' + row + ' ></span></td> '
str+= ' <td align= ' center ' ><span id= ' + ' B ' + row + ' ></span></td> '
str+= ' <td align= ' center ' ><span id= ' + ' C ' + row + ' ></span></td> '
str+= ' <td align= ' center ' ><span id= ' + ' d ' + row + ' ></span></td> '
str+= ' <td align= "center" ><span id= "' +" E "+ row + ' ></span></td> '
str+= ' <td align= ' center ' ><span id= ' + ' f ' + row + ' ></span></td> '
str+= ' <td align= "center" ><span id= "' +" G "+ row + ' ></span></td> '
str+= ' <td align= ' center ' ><span id= ' + ' h ' + row + ' ></span></td> '
str+= ' <td align= ' center ' ><span id= ' + ' I ' + row + ' ></span></td> '
str+= ' </tr> ';
$ ("#tablebody"). Append (str);
}
</script>
<style>
. tr_cls {...} {
height:30px;
font-size:16px;
Font-family: "Times New Roman", the Times, serif;
Background-color: #FFFFCC
}
</style>
<body>
<form>
<input type= "hidden" id= "Stockurl" value= "http://hq.sinajs.cn/list="/>
<input type= "hidden" id= stockinit "value=" sh000001,sz399001,sh601939,sh600016,sh600528,sh600667,sh601390, sh601857,sz000532,sh600019,sh601111,sh601600,sh601006,sh601333,sh601398,sh601988,sh601328 "/>
<table width= "border=" 0 "align=" center "cellpadding=" 0 "cellspacing=" 0 ">
<tr>
&LT;TD width= "115px" >
Code: <input type= "text" id= "Stockid" size= "6" >
</td>
&LT;TD width= "150px" >
Distinction: Shanghai <input type= "Radio" Name= "Rad" id= "sh_id" checked> deep proof <input type= "Radio" Name= "Rad" id= "sz_id" >
</td>
&LT;TD width= "40px" >
<input type= "button" id= "Insrow" value= "line append" >
</td>
&LT;TD width= "495px" >
<input type= "button" id= "Delrow" value= "line Cut" >
</td>
</tr>
<tr>
&LT;TD colspan= "4" >
<div style= "Overflow-y:scroll; Overflow-x:auto; height:392px; Width:817px ">
<table id= "TableId" width= "border=" 1 "align=" center "cellpadding=" 0 "cellspacing=" 0 "bordercolor=" #000000 " >
<tr bgcolor= "#3399FF" height= "30px" >
<th scope= "col" > Stock code </th>
<th scope= "col" > Stock name </th>
<th scope= "col" > Current price </th>
<th scope= "col" > Today opening </th>
<th scope= "col" > Yesterday closing </th>
<th scope= "col" > Current price difference </th>
<th scope= "col" > Degree </th>
<th scope= "col" > Highest price </th>
<th scope= "col" > Lowest price </th>
</tr>
<tbody id= "Tablebody" >
</tbody>
</table>
</div>
</td>
</tr>
</table>
</form>
</body>
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.