在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
首先先看实现代码 html代码部分 <!DOCTYPE html> <html> <head> <title>Previewing Links</title> <link rel="stylesheet"href="script05.css" rel="external nofollow" > <script src="script05.js"></script> </head> <body> <h2>A Gentle Introduction to JavaScript</h2> <ul> <li><a href="jsintro/2000-08.html" rel="external nofollow" >August column</a></li> <li><a href="jsintro/2000-09.html" rel="external nofollow" >September column</a></li> <li><a href="jsintro/2000-10.html" rel="external nofollow" >October column</a></li> <li><a href="jsintro/2000-11.html" rel="external nofollow" >November column</a></li> </ul> <div id="previewWin"> </div> </body> </html> 这个CSS设置预览弹出窗口的样式 #previewWin { background-color: #FF9; width: 400px; height: 100px; font: .8em arial, helvetica, sans-serif; padding: 5px; position: absolute; visibility: hidden; top: 10px; left: 10px; border: 1px #CC0 solid; clip: auto; overflow: hidden; } #previewWin h1, #previewWin h2 { font-size: 1.0em; } 这个JavaScript进行服务器请求并且显示弹出窗口 window.onload = initAll; var xhr = false; var xPos, yPos; function initAll() { var allLinks = document.getElementsByTagName("a"); for (var i=0; i< allLinks.length; i++) { allLinks[i].onmouseover = getPreview; } } function getPreview(evt) { if (evt) { var url = evt.target; } else { evt = window.event; var url = evt.srcElement; } xPos = parseInt(evt.clientX); yPos = parseInt(evt.clientY); if (window.XMLHttpRequest) { xhr = new XMLHttpRequest(); } else { if (window.ActiveXObject) { try { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { } } } if (xhr) { xhr.onreadystatechange = showContents; xhr.open("GET", url, true); xhr.send(null); } else { alert("Sorry, but I couldn't create an XMLHttpRequest"); } } function hidePreview() { document.getElementById("previewWin").style.visibility = "hidden"; } function showContents() { var prevWin = document.getElementById("previewWin"); if (xhr.readyState == 4) { if (xhr.status == 200) { prevWin.innerHTML = xhr.responseText; } else { prevWin.innerHTML = "There was a problem with the request " + xhr.status; } prevWin.style.top = yPos+2 + "px"; prevWin.style.left = xPos+2 + "px"; prevWin.style.visibility = "visible"; prevWin.onmouseout = hidePreview; } } 分析: 1. var allLinks = document.getElementsByTagName("a"); for (var i=0; i< allLinks.length;i++) { allLinks[i].onmouseover = getPreview; } 这是initAll()函数的内容,它遍历页面上的所有链接,并且在每个链接上添加onmouseover事件 if (evt) { var url = evt.target; } else { evt = window.event; var url = evt.srcElement; } xPos = parseInt(evt.clientX); yPos = parseInt(evt.clientY); 在getPreview()中,首先需要查明要读取哪个文件,这就要查看事件的属性。根据访问者使用的 3. function hidePreview() { document.getElementById ("previewWin").style.visibility = "hidden"; } 如果打算显示预览,将需要再次隐藏它,对吗?hidePreview()函数的作用是将预览窗口的可见性 if (xhr.status == 200) { prevWin.innerHTML = xhr.responseText; } else { prevWin.innerHTML = "There was a problem with the request " + xhr.status; } prevWin.style.top = yPos+2 + "px"; prevWin.style.left = xPos+2 +"px"; prevWin.style.visibility ="visible"; prevWin.onmouseout = hidePreview; 如果一切正常,那么xhr.status为200,而且我们希望放在prevWin.innerHTML中的数据已经存 |
请发表评论