jquerystock.js:
//进入页面以后就显示股票当前的价格信息
var obj;
var sid;
$(document).ready(function(){
var stockNode = $("#stock").css("border","1px solid green").css("background","blue")
.css("color","red").css("width","150px").css("position","absolute"); //只有将Position属性定义为absolute才可以在想要的位置出现对话框
stockNode.hide();
getStockInfo();
//每隔一秒与服务器交互一次,页面自动刷新当前的股票价格信息
setInterval(getStockInfo,1000);
var as = $("a");
//定义鼠标划入的事件
as.mouseover(function(){
var aNode = $(this);
var divNode = aNode.parent();
sid = divNode.attr("id");
//updatediv();
//控制弹出框的位置
//1.查处链接的位置
var offset = aNode.offset();
//2.设置弹出框的位置
stockNode.css("left",offset.left + "px").css("top",offset.top + aNode.height() + "px");
stockNode.show();
});
//定义鼠标划出的事件
as.mouseout(function(){
stockNode.hide();
});
});
function getStockInfo(){
//1.向服务器发送请求,得到数据
$.get("GetStackInfo",null,function(data){
//2.解析从服务器返回的数据
obj = eval(data);
//3.根据主键得到对象
var szzs = obj["300001"];
var pfyh = obj["000001"];
var span3 = $("#300001").children("span");
span3.html(szzs.now);
if(szzs.now > szzs.yes){
span3.css("color","red");
}else{
span3.css("color","green");
}
var span1 = $("#000001").children("span");
span1.html(pfyh.now);
if(pfyh.now > pfyh.yes){
span1.css("color","red");
}else{
span1.css("color","green");
}
//每隔一秒获得新的obj对象,就改变"stock" div 的有关属性
updatediv();
});
}
//更新显示框中的内容
function updatediv(){
var stockObj = obj[sid];
for(var prop in stockObj){
if(prop != "name"){
$("#"+ prop).children("span").html(stockObj[prop]);
}
}
}
Stock.java
/**
* Created by IntelliJ IDEA.
* User: lucky
* Date: 2009-8-16
* Time: 18:03:42
* To change this template use File | Settings | File Templates.
*/
public class Stock {
public Stock(double yestaday, double today, String id, String name) {
this.yestaday = yestaday;
this.today = today;
this.id = id;
this.name = name;
this.now = today;
}
/**
* 昨天的收盘价
*/
private double yestaday;
/**
* 今天的开盘价
*/
private double today;
/**
* 现在的价格
*/
private double now;
/**
* 股票代码
*/
private String id;
/**
* 股票名称
*/
private String name;
public double getToday() {
return today;
}
public double getNow() {
return now;
}
public String getName() {
return name;
}public void setYestaday(double yestaday) {
this.yestaday = yestaday;
}
public void setToday(double today) {
this.today = today;
}
public void setNow(double now) {
this.now = now;
}
public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public double getYestaday() {
return yestaday;
}
public String toString(){
return this.name + ":" + this.now;
}
}
JQueryStock.html:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquerystock.js"></script>
<title></title>
</head>
<body>
<div id="300001"><a href="#">上证指数</a><span></span></div>
<div id="000001"><a href="#">浦发银行</a><span></span></div>
<div id="stock">
<div id="yes"><span></span></div>
<div id="tod"><span></span></div>
<div id="now"><span></span></div>
</div>
</body>
</html>