簡體   English   中英

Document.ready()函數

[英]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 / class來運行ajax函數:

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

但是我不確定我的代碼在哪里實現

如果要在多個瀏覽器上運行代碼,則編寫自己的ajax請求不是一個好主意。 如果您手上有jQuery,並且想要發布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);
        })
    });
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM