Ajax实现提示功能
来源:互联网 发布:大唐天下网络骗局 编辑:程序博客网 时间:2024/05/16 08:20
有过网购的同学都知道,当我们在 网上买某一件东西的时候,如果我们把鼠标放置到图片的上方,那么会弹出一个框框,介绍该产品的信息,下面我们来模拟一下这个功能
先写一个shop.js
// JavaScript Documentvar xmlHttpRequest;function createXmlHttpRequest(){if(window.ActiveXObject){xmlHttpRequest=new ActiveXObject("Microsoft.XMLHTTP");}else (window.XMLHttpRequest){xmlHttpRequest=new XMLHttpRequest();}}function over(index){//alert("over执行");x=event.clientX;y=event.clientY;createXmlHttpRequest();xmlHttpRequest.onreadystatechange=processor;xmlHttpRequest.open("GET","ShopServlet?index="+index,true);xmlHttpRequest.send(null);}function processor(){//alert("1");if(xmlHttpRequest.readyState==4){//alert("2");var result;if(xmlHttpRequest.status==200){//alert("2");result=xmlHttpRequest.responseXML.getElementsByTagName("shop");document.getElementById("tip").style.display="block";document.getElementById("tip").style.top=y;document.getElementById("tip").style.left=x+10;document.getElementById("gname").innerHTML="商品名称:"+result[0].childNodes[0].firstChild.nodeValue;document.getElementById("gprice").innerHTML="商品价格:"+result[0].childNodes[1].firstChild.nodeValue;//alert("商品名称:"+result[0].childNodes[0].firstChild.nodeValue);}}}function out(){doucment.getElementById("tip").style.display="none";}
然后写一个jsp页面,shop.jsp
body中的代码:
<h2>使用Ajax实现提示功能</h2> <hr> <a href="#" onmouseover="over(0)" onmouseout="out()">商品1</a> <a href="#" onmouseover="over(1)" onmouseout("out()")>商品2</a> <a href="#" onmouseover="over(2)" onmouseout("out()")>商品3</a> <div id="tip" style="position: absolute;display: none;border: 1px;border-style: solid;"><table id="tiptable" border="0" bgcolor="#ffffee"><tr><td id="gname"></td></tr><tr><td id="gprice"></td></tr></table>
最后编写一个Servlet
public class ShopServlet extends HttpServlet{public void destroy(){super.destroy(); // Just puts "destroy" string in log// Put your code here}public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{this.doPost(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{System.out.println("servlet执行");response.setContentType("text/xml");response.setCharacterEncoding("utf-8");String[][]info={{"苹果","10"},{"香蕉","8"},{"梨子","4"}};int index=Integer.valueOf(request.getParameter("index"));PrintWriter pw=response.getWriter();pw.println("<shop>");pw.println("<name>"+info[index][0]+"</name>");pw.println("<price>"+info[index][1]+"</price>");pw.println("</shop>");pw.flush();pw.close();}/** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */public void init() throws ServletException{// Put your code here}}
运行后的界面如下:
- Ajax实现提示功能
- AJAX实现随笔提示功能
- AJAX实现文本框输入自动提示功能
- Ajax应用---实现自动提示功能
- ajax实现搜索提示功能 (关键字自动匹配功能)
- Ajax自动提示功能
- jQuery(Ajax)/PHP/MySQL实现搜索自动提示功能
- 用jQuery + Ajax实现Google输入提示功能
- 用jQuery + Ajax实现Google输入提示功能
- 用 jQuery + Ajax 实现 Google 的输入提示功能
- 运用ajax实现仿谷歌搜索输入提示功能(c#)
- jquery ajax 实现搜索框自动提示功能
- jQuery Ajax实现简单的搜索框提示功能
- [转] 利用AJAX实现搜索提示功能 (关键字自动匹配功能)
- Ajax实现搜索功能
- ajax实现保存功能
- JQuery实现Ajax功能
- struts2实现ajax功能
- Simplified Approach to Resolve ORA-4031
- Unity 陀螺仪。。哈哈。。。。
- Activity中Dialog创建流程以及SparseArray分析
- 一个游戏程序员的学习资料
- Shell Script中FOR循环的使用
- Ajax实现提示功能
- 黑马程序员——各种for循环语句的一个总结
- PKU 1166 The Clocks
- HDU 4320 - Arcane Numbers 1 / SWUN 1429 - 进制转化
- STL问题与解决(1)(刚开始)
- spring声明式事务支持7种传播行为
- Unity 再次谈优化问题。三合一。。感谢分享。。
- KMP算法
- PHP相关系列 - 浅析http协议、cookies和session机制、浏览器缓存 .