<!DOCTYPE HTML PUBLIC "-//W3C//DTD Html 4.01 Transitional//EN">
<Html>
<head>
<title>AJax工具條提示</title>
<script type="text/Javascript">
var XMLHttp;
var dataDiv;
var dataTable;
var dataTableBody;
var offsetEl;
//創建XMLHttpRequest對象
function createXMLHttpRequest() {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}
//初始化變量值
function initVars() {
dataTableBody = document.getElementById("courseDataBody");
dataTable = document.getElementById("courseData");
dataDiv = document.getElementById("popup");
}
//得到課程數據
function getCourseData(element) {
initVars();
createXMLHttpRequest();
offsetEl = element;
var url = "ToolTipServlet?key=" + escape(element.id);
XMLHttp.open("GET", url, true);
XMLHttp.onreadystatechange = callback;
XMLHttp.send(null);
}
//回調方法
function callback() {
if (XMLHttp.readyState == 4) {
if (XMLHttp.status == 200) {
setData(xmlHttp.responseXML);
}
}
}
//設置課程數據
function setData(courseData) {
clearData();
setOffsets();
var length = courseData.getElementsByTagName("length")[0].firstChild.data;
var par = courseData.getElementsByTagName("par")[0].firstChild.data;
var row, row2;
var parData = "Par: " + par
var lengthData = "Length: " + length;
row = createRow(parData);
row2 = createRow(lengthData);
dataTableBody.appendChild(row);
dataTableBody.appendChild(row2);
}
//創建表格行
function createRow(data) {
var row, cell, txtNode;
row = document.createElement("tr");
cell = document.createElement("td");
cell.setAttribute("bgcolor", "#FFFAFA");
cell.setAttribute("border", "0");
txtNode = document.createTextNode(data);
cell.appendChild(txtNode);
row.appendChild(cell);
return row;
}
//設置坐標偏移量
function setOffsets() {
var end = offsetEl.offsetWidth;
var top = calculateOffsetTop(offsetEl);
dataDiv.style.border = "black 1px solid";
dataDiv.style.left = end + 15 + "px";
dataDiv.style.top = top + "px";
}
//計算頂部坐標偏移量
function calculateOffsetTop(fIEld) {
return calculateOffset(fIEld, "offsetTop");
}
//計算坐標偏移量
function calculateOffset(fIEld, attr) {
var offset = 0;
while(fIEld) {
offset += fIEld[attr];
field = fIEld.offsetParent;
}
return offset;
}
//清空數據
function clearData() {
var ind = dataTableBody.childNodes.length;
for (var i = ind - 1; i >= 0 ; i--) {
dataTableBody.removeChild(dataTableBody.childNodes[i]);
}
dataDiv.style.border = "none";
}
</script>
</head>
<body>
<h1>AJax Tooltip Example</h1>
<h3>Golf Courses</h3>
<table id="courses" bgcolor="#FFFAFA" border="1" cellspacing="0" cellpadding="2"/>
<tbody>
<tr><td id="1" >Augusta National</td></tr>
<tr><td id="2" >Pinehurst No. 2</td></tr>
<tr><td id="3" >St. Andrews Links</td></tr>
<tr><td id="4" >Baltusrol Golf Club</td></tr>
</tbody>
</table>
<div style="position:absolute;" id="popup">
<table id="courseData" bgcolor="#FFFAFA" border="0" cellspacing="2" cellpadding="2"/>
<tbody id="courseDataBody"></tbody>
</table>
</div>
</body>
</Html>
服務器:ToolTipServlet.Java
package AJaxbook.chap4;
import Java.io.*;
import Java.util.HashMap;
import Java.util.Map;
import Javax.servlet.*;
import Javax.servlet.http.*;
public class ToolTipServlet extends HttpServlet {
//課程集合
private Map courses = new HashMap();
//初始化集合數據
public void init(ServletConfig config) throws ServletException {
CourseData augusta = new CourseData(72, 7290);
CourseData pinehurst = new CourseData(70, 7214);
CourseData standrews = new CourseData(72, 6566);
CourseData baltusrol = new CourseData(70, 7392);
courses.put(new Integer(1), augusta);
courses.put(new Integer(2), pinehurst);
courses.put(new Integer(3), standrews);
courses.put(new Integer(4), baltusrol);
}
/**
* 處理Get方法
* @param request HttpServletRequest
* @param response HttpServletResponse
* @throws ServletException
* @throws IOException
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//得到參數key
Integer key = Integer.valueOf(request.getParameter("key"));
System.out.println("key:" + key.toString());
CourseData data = (CourseData) courses.get(key);
//構建返回字符串
PrintWriter out = response.getWriter();
response.setContentType("text/XML");
response.setHeader("Cache-Control", "no-cache");
out.println("<response>");
out.println("<par>" + data.getPar() + "</par>");
out.println("<length>" + data.getLength() + "</length>");
out.println("</response>");
out.close();
}
/** Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
/** Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}
/**
* 內部類,課程
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
private class CourseData {
private int par;
private int length;
public CourseData(int par, int length) {
this.par = par;
this.length = length;
}
public int getPar() {
return this.par;
}
public int getLength() {
return this.length;
}
}
}