php中的Ajax查找正确的div/class

php中的Ajax查找正确的div/class,php,javascript,html,css,class,Php,Javascript,Html,Css,Class,正如评论所指出的,您不应该在PHP标记中使用jQuery。在页面头部添加一些脚本标记,添加document.ready处理程序,然后将脚本放入其中 请注意,一旦完成此操作,您仍然会遇到一些问题,因为您的this值混淆了 $('.eventcontainer.button').click(function() { $.post('javas.php', function(data) { $(this).parent('div').find('.status').html(data);

正如评论所指出的,您不应该在PHP标记中使用jQuery。在页面头部添加一些脚本标记,添加document.ready处理程序,然后将脚本放入其中

请注意,一旦完成此操作,您仍然会遇到一些问题,因为您的
this
值混淆了

  $('.eventcontainer.button').click(function() { 
$.post('javas.php', function(data) {
   $(this).parent('div').find('.status').html(data);
})
});


hr.open("POST", url, true);

// Set content type header information for sending url encoded variables in the request
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// Access the onreadystatechange event for the XMLHttpRequest object
hr.onreadystatechange = function() {
    if(hr.readyState == 4 && hr.status == 200) {
        var return_data = hr.responseText;
        document.getElementById("status").innerHTML = return_data;
    }
}
// Send the data to PHP now... and wait for response to update the status div
hr.send("num=" + (++num)); // Actually execute the request
document.getElementById("status").innerHTML = "processing...";

}
一旦您点击了
$.post
调用的回调,
不再等于您单击的项目。在调用post之前,需要将该元素保存在局部变量中

差不多

$('.eventcontainer.button').click(function() { 
   $.post('javas.php', function(data) {
      $(this).parent('div').find('.status').html(data); 
   })
});

你有什么错误?您尝试过什么?为什么在PHP标记中有jQuery?我应该把它放在jQuery标记中而不是PHP中吗?@foshoeiyyy-我不是PHP爱好者,但我猜它不应该放在PHP标记中。@foshoeiyy-把这段代码放在您的jQuery文档中。准备好了吗handler@foshoeiyyy只有PHP应该在PHP标记中,除非你在回应和/或打印什么。您的javascript/jQuery应该在
标记中。@foshoeiyyy-添加一个。在标题中创建一个脚本标记,并在其中放置一个。看看这个
$('.eventcontainer.button').click(function() { 
   $.post('javas.php', function(data) {
      $(this).parent('div').find('.status').html(data); 
   })
});
$('.eventcontainer.button').click(function() { 
    var self = this;
    $.post('javas.php', function(data) {
      $(self).parent('div').find('.status').html(data);
   })
});