0

My problem is that my code is really long and complicated, because it's normal xmlhttp and I want to switch it over to jQuery. I'm pretty sure there's shorthand functions for it? This is my current code:

function getContent() {
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    } else {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {

            replace_page = xmlhttp.responseText;
            doc.getElementById("content").innerHTML = replace_page;

            $("#content").children().hide();
            $("#content").children().fadeIn("slow");

            //do some other stuff
        }
        navigating = false;
    };
    xmlhttp.open("GET",site_location+"/pages/"+page+".php",true);
    xmlhttp.send();
}

I found this example, and this works perfectly, but I'm also going to have to use this for forms...

Is there a simple way I can change it? I know it'll probably involve the serialising function, right?

4

1 回答 1

2

看看$.ajax()

$.ajax({
    type: 'GET',
    url: site_location+"/pages/"+page+".php",
}).done(function(html){
    $('#content').html(html).children().hide().fadeIn("slow");
})

或更好地使用load()因为目标是将 html 内容加载到目标元素

$('#content').load(site_location+"/pages/"+page+".php", function(){
    $(this).children().hide().fadeIn("slow");
})
于 2013-10-03T02:25:25.387 回答