我正在尝试在鼠标悬停时使用 ajax 从文件中获取数据。一切正常,除了当我尝试访问<p>
匿名函数中的元素时,我什么也得不到。可能的原因是元素丢失了匿名函数内的范围。如果您看到可能的解决方案,请告知。
<html>
<head>
<title>MouseOver Effect And Ajax </title>
<link rel="stylesheet" type="text/css" href="style.css"/>
<script type="text/javascript" src="http://localhost/study/libraries/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var xhr=false;
initAll();
$('div.pcard').mouseover(function(){
if(xhr)
{
var pname=$(this).children('p.pname').text();
var pname=pname+"_details.txt";
xhr.open("GET",pname);;
xhr.onreadystatechange=function(){
if(xhr.readyState==4)
{
if(xhr.status==200)
{
$(this).children('p.pdesc').text(""+msg);
alert($(this).children('p.pname').text());
$(this).children('p.pdesc').css({'visibility':'visible'});
}
}
}.bind(this);
xhr.send(null);
}
});
$('div.pcard').mouseout(function(){
$(this).children('p.pdesc').css({'visibility':'hidden'});
});
function initAll()
{
if(window.XMLHttpRequest)
{
xhr=new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
try{
xhr=new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){}
}
}
});
</script>
</head>
<body>
<h2>Interactive MouseOver</h2>
<div id="products">
<div class="pcard">
<p class="pname">Sandwhiches</p>
<p class="pdesc"></p>
</div>
<div class="pcard">
<p class="pname">Pizzas</p>
<p class="pdesc"></p>
</div>
<div class="pcard">
<p class="pname">Soups</p>
<p class="pdesc"></p>
</div>
<p style="clear:both"></p>
</div>
</body>
</html>