Ajax實現工具條提示

來源:互聯網
上載者:User
ajax|工具條

    頁面:toolTip.jsp

<!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;
        }
    }
}



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.