我想在单击图像时将名为john的div的内容更改为php文件的输出。我确定这很简单,但我找不到适合的例子。这里是我的代码:使用Ajax更改div onclick的内容
function ajaxFunction(){
var ajaxRequest;
ajaxRequest = new XMLHttpRequest();
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
document.getElementById("john").innerHTML=xmlhttp.responseText;
}
}
ajaxRequest.open("GET", "test.php", true);
ajaxRequest.send(null);
}
HTML
<img class='cross' src='images/cross.png' onclick="ajaxFunction();">
<div id='john'>john</div>
test.php的
<?php echo "hello"; ?>
谢谢。很好的与'xmlhttp'搭档,但我试过了你的代码,它仍然没有响应点击。 –