Document.ready() function


Document.ready() function

这是我的ajax函数

<script language="JavaScript" type="text/javascript">
 var num = 1;
function ajax_post(){
// Create our XMLHttpRequest object
var hr = new XMLHttpRequest();
// Create some variables we need to send to our PHP file
var url = "javas.php";

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...";
}

现在我有这个找到正确的div/类来运行ajax函数:

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

然而,我不确定在哪里实现这在我的代码

如果您希望在多个浏览器上运行代码,那么编写自己的ajax请求并不是一个好主意。如果你手头有jQuery并且你想要post ajax请求使用jQuery函数:

$.post('ajax/test.html', function(data) {
    $('.result').html(data);
});

准备使用的文档示例:

function fooBar() {
//some code
}
$(document).ready(function(){
// all your jquery in here
$('body').hide().fadeIn(2000);
// or call your own functions
fooBar();
});

你可以这样做:

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