使用Ajax更改div onclick的内容

使用Ajax更改div onclick的内容,ajax,onclick,Ajax,Onclick,我想在单击图像时将名为john的div的内容更改为php文件的输出。我相信这很简单,但我找不到一个合适的例子。这是我的密码: function ajaxFunction(){ var ajaxRequest; ajaxRequest = new XMLHttpRequest(); ajaxRequest.onreadystatechange = function(){

我想在单击图像时将名为john的div的内容更改为php文件的输出。我相信这很简单,但我找不到一个合适的例子。这是我的密码:

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


厕所
test.php

<?php echo "hello"; ?>
<?php echo "hello".$_GET["q"];  ?>

在我看来,您的javascript有语法错误。您试图从
xmlhttp
获取
responseText
,但是
XMLHttpRequest
存储在
ajaxRequest

试试这个

function ajaxFunction(){
    var ajaxRequest = new XMLHttpRequest();
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4 && ajaxRequest.status==200){
                document.getElementById("john").innerHTML=ajaxRequest.responseText;
            }
        }
    ajaxRequest.open("GET", "test.php", true);
    ajaxRequest.send(); 
}

对于跨浏览器/版本,我可以建议使用:

var ajaxRequest = getXMLHttpRequest();
然后呢,

function getXMLHttpRequest() {
    var re = "nothing";
    try {re = new window.XMLHttpRequest();} catch(e1) {};
    try {re = new ActiveXObject("MSXML2.XMLHTTP.4.0");} catch(e) {};
    try {re = new ActiveXObject("Msxml2.XMLHTTP");} catch(e2) {};
    try {re = new ActiveXObject("Microsoft.XMLHTTP");} catch(e3) {};
    try {re = new ActiveXObject("MSXML2.XMLHTTP.3.0");} catch(ex) {};
    if (re != "nothing") {return re;}
    else {return null;};
};
以及将其更改为

ajaxRequest.responseText();

我已经用下面的代码解决了这个问题。在从php文件中获取“hello”后,此代码还返回元素的ID

<script language="javascript">
            function calldislike(str){
                if (str==""){
                  document.getElementById("john").innerHTML="";
                  return;
                  }
                xmlhttp=new XMLHttpRequest();
                xmlhttp.onreadystatechange=function(){
                    if (xmlhttp.readyState==4 && xmlhttp.status==200){
                    document.getElementById("john").innerHTML=xmlhttp.responseText;
                    }
                }
                xmlhttp.open("GET","test.php?q="+str,true);
                xmlhttp.send();
            }
        </script>

<img src='images/cross.png' onclick="calldislike(this.id);" id='3'>
<div id='john'>john</div>

函数调用(str){
如果(str==“”){
document.getElementById(“john”).innerHTML=“”;
返回;
}
xmlhttp=新的XMLHttpRequest();
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
document.getElementById(“john”).innerHTML=xmlhttp.responseText;
}
}
open(“GET”、“test.php?q=“+str,true”);
xmlhttp.send();
}
厕所
test.php

<?php echo "hello"; ?>
<?php echo "hello".$_GET["q"];  ?>


谢谢。“xmlhttp”很好,但我已经尝试了您的代码,但它仍然没有响应单击。谢谢。我的访客中只有3%似乎使用旧版本的IE,所以现在我只专注于让它发挥作用,但我想当我让它发挥作用的时候,解决这个问题不会有什么坏处。