Truly
写精彩代码 品暇逸人生
作者:Truly
日期:2007.7.31

近期Post了不少Javascript文章,还是收到不少支持,你们的回复是我写下去的动力,谢谢你们!

刚刚研究了一下新浪财经板块,其中对股票数据的处理真的是跟我的MVC模式的文章不谋而合啊。我顺手写了2段代码,大家看看。

Stock.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
    
<title>Truly's stock</title>
    
<script src=data.js></script>
    
<style>
/* change_color */
.incolor
{color:#FF0000;}
.decolor
{color:#008000;}
#itemTickettime
{
    font-size
:12px;
}
span,div
{font-size:12px;}
    
</style>
</head>

<body>
<script id=vsdata></script>
<input id=mystock value='000001'><br><br>
<input id='sh' type=radio name='type' checked value=1>sh<input type=radio name='type' value=2>sz
<br><br>
<input type=button value=' Go ' onclick='loadMyStockList()'> <input type=button value=' Stop ' onclick='stopFresh()'><br><br>
<div class="tbtb01">
<span id='name'></span>
<div><span id="itemMarket">-</span> <span id='code'></span> &nbsp;&nbsp;昨收<span id='s'></span>&nbsp;&nbsp;今开<span id='k'></span></div>
</div>
<div class="tbtb02">
<h3 id="itemCurrent" class="incolor">-</h3>
<span class="fRed01" id="itemcurrprice">-</span>&nbsp;&nbsp;<span class="fRed01" id="itemDiffpercent">-</span>
<span id="itemTickettime">-</span>
</div>
</body>
</html>

Data.js
var AppConf = {
    Interval_1s:
1*1000,
    Interval_AginReq:
500,
    url:'http:
//hq.sinajs.cn/format=js&func=S_Finance.upconstants.setData();&list='

};

function $(id){return document.getElementById(id);};
function $C(nodeName){return document.createElement(nodeName);};
function gt(){if($('sh').checked) return 'sh'+$('mystock').value;return 'sz'+$('mystock').value;}
var timer;
var itemCurrent;
function loadMyStockList()
{
    itemCurrent 
= $("itemCurrent");
    fullcode
=gt();
    clearInterval(timer);
    timer 
= setInterval("Truly.load_data_script(AppConf.url+gt(),'vsdata');"1000);
}
function stopFresh()
{
    clearInterval(timer);
}
var jk,zs;
var currprice,currnum;
var _A=function(t,c){t.appendChild(c)};
var fullcode = 'sh600390';
var Truly = {
    load_data_script:
function(url,idname)
    {
        
var h = new Date().getHours();
        
var m = new Date().getMinutes();
        
if(h < 9 || (h>15|| (h == 15&& m>10)) stopFresh();
        
var obj=$(idname);
        
if(obj){obj.parentNode.removeChild(obj);};
        
var newscript=$C("script");
        newscript.type
="text/javascript";
        newscript.src
=url;
        newscript.id
=idname;
        _A(document.body,newscript);
    }
}
var S_Finance={};
S_Finance.upconstants
={itemTickettime:null,servertime:null,
setData:
function(){
    
this.itemTickettime=$("itemTickettime").innerHTML;
    eval(
"this.hq_str_symbol = hq_str_"+fullcode);
    
if(this.hq_str_symbol!=null && this.hq_str_symbol!=undefined && this.hq_str_symbol!="undefined" && this.hq_str_symbol!="")
    {
        
this.chgedary=new Array();
        
this.chgedarylen=0;
        
this.cacheidx=0;
        
this.setValue(this.hq_str_symbol);
        
this.initflag=1;
    }
    
else
    {
        setTimeout(
this._updata._Bind(this),AppConf.Interval_AginReq);
        
return true;
    };
    
this.setFlash=function(tid)
    {
        
var _tidColor=$(tid).style.color;
        setTimeout(
function(){$(tid).style.color="#000000";},400);
        setTimeout(
function(){$(tid).style.color=_tidColor;},600);
        setTimeout(
function(){$(tid).style.color="#000000";},800);
        setTimeout(
function(){$(tid).style.color=_tidColor;},1000);
    };
    
if(this.itemTickettime!=$("itemTickettime").innerHTML)
    {
        $(
"itemcurrprice").style.fontFamily="Arial";
        $(
"itemcurrprice").style.fontSize="11px";
        $(
"itemDiffpercent").style.fontFamily="Arial";
        $(
"itemDiffpercent").style.fontSize="11px";
        
this.setFlash("itemCurrent");
        
this.setFlash("itemcurrprice");
        
this.setFlash("itemDiffpercent");
    };
    
return true;
},
    setValue:
function(data)
    {
        
var dataContent=data;
        
if(dataContent){
            dataContent.replace(
"\r","");dataContent.replace("\n","");
            
var dataArray=dataContent.split(",");
            
var arrayLen=dataArray.length;
        }
        $(
"itemTickettime").innerHTML=dataArray[30]+" "+dataArray[31];
        $(
"itemCurrent").innerHTML=dataArray[3];
        $(
"name").innerHTML=dataArray[0];
        $(
"code").innerHTML=fullcode;
        currprice 
= (dataArray[3]*1000-dataArray[2]*1000)/1000;
        currnum
=(dataArray[3]*1000-dataArray[2]*1000)/(10*dataArray[2]);
        $(
"itemcurrprice").innerHTML=currprice;
        $(
"itemDiffpercent").innerHTML= Math.round(currnum*100)/100 + '%';
        jk
=dataArray[1];
        zs
=dataArray[2];
        
        $(
"k").innerHTML=jk;
        $(
"s").innerHTML=zs;
        
if(dataArray[3>zs) itemCurrent.style.color='#FF0000';
        
else if(dataArray[3==zs) itemCurrent.style.color='#000000';
        
else itemCurrent.style.color='#008000';
        $(
"itemcurrprice").style.color=itemCurrent.style.color;
        $(
"itemDiffpercent").style.color=itemCurrent.style.color;
    }
}

呵呵,写的很简单就不往首页放了。大家慢慢看,玩股票的兄弟们祝你们都赚到钱,不玩股票的就多研究研究技术。这里下载源码。

enjoy code!

posted on 2007-07-31 22:17  Truly  阅读(1511)  评论(3编辑  收藏  举报