[英]AJAX Mouse Over Image, Display HTML Content
将鼠标悬停在图像上时,我尝试使用AJAX显示html内容。 我的代码似乎不错,但似乎无法正常工作。 deakin-campus / discover-deakin是我要显示的html页面。
下面的代码不显示任何内容,但是如果我在整个代码中添加了一些警报, 显示html文本。
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<script type="text/javascript" >
var asyncRequest;
function getContent(url)
{
try
{
asyncRequest = new XMLHttpRequest();
asyncRequest.open( 'GET', url, true );
asyncRequest.send(null);
asyncRequest.onreadystatechange = stateChange();
}
catch (exception)
{
alert("error");
}
}
function stateChange()
{
if (asyncRequest.readyState == 4 && asyncRequest.status == 200)
{
document.getElementById('ContentArea').innerHTML= asyncRequest.responseText;
}
}
function clearContent()
{
document.getElementById('ContentArea').innerHTML = '';
}
</script>
</head>
<body>
<img src="deakin-campus.jpg" width="71" height="71"
onmouseover = 'getContent("deakin-campus.html")'
onmouseout = 'clearContent()' />
<img src="discover-deakin.jpg" width="71" height="71"
onmouseover = 'getContent("discover-deakin.html")'
onmouseout = 'clearContent()' />
<div id="ContentArea"> </div>
</body>
</html>
通过函数引用...因此没有“()”。
asyncRequest.onreadystatechange = stateChange;
还要在调用.send方法之前执行此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.