我的问题是我的代码真的又长又复杂,因为它是普通的 xmlhttp,我想将它切换到 jQuery。我很确定它有简写函数?这是我当前的代码:
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();
}
我找到了这个example ,这工作得很好,但我也必须将其用于表单......
有没有简单的方法可以改变它?我知道它可能会涉及序列化功能,对吗?
最佳答案
看$.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");
})
关于javascript - 将 xmlhttp javascript 转换为 jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19149769/