我很难得到合作的请求。我想首先说PHP方面完美无缺。我现在正在尝试使用ajax和jQuery使其看起来流畅。
这是js:
$(".controls a").click(function(e) {
e.preventDefault();
$.get($(this).attr("href"), function(data) {
$(".schedule").fadeOut(200, function() {
$(this).html(data).fadeIn(200);
});
});
});
$(this).attr("href")
是指为了从MySQL数据库中获取信息而传递的URL(例如hours.php?week=2014-08-11
)。每次点击链接时,week
中传递的值都会通过PHP更新。
以下是我从console.log(data)
http://pastebin.com/bGpfjq6r
我尝试通过执行以下操作将data
(原始HTML)转换为jQuery对象:
var $data = $.parseHTML(data);
然而,这只是将HTML转换为数组。我可以为元素执行find
:
console.log($($data).find(".schedule"));
但是当我查看输出时,context
未定义。
我也在这个问题Extract part of HTML document in jQuery中尝试了接受的答案但没有结果:
var foo = $(".schedule", $data);
console.log(foo);
这仍有未定义的context
。
理想情况下,我想只抓取.section
中的信息,并将.section
中的当前信息替换为从GET请求中捕获的信息。 .section
是document
的一部分,也是请求返回的内容。
控制台中没有错误。 jQuery版本是1.11.1。
如果问题写得不好,我道歉。我试着用一般的方式写它,所以它也适用于其他人。如果您需要其他信息,请与我们联系。
答案 0 :(得分:0)
尝试使用jQuery filter()函数。
$(".controls a").click(function(e) {
e.preventDefault();
$.get($(this).attr("href"), function(data) {
$(".schedule").fadeOut(200, function() {
// apply filter function here to find new schedule div
var newSchedule = $(data).filter(".schedule").eq(0).html();
$(this).html(newSchedule).fadeIn(200);
});
});
});
答案 1 :(得分:0)
尝试包装响应,以便您可以解析它......
$(document).ready(function() {
// Create named function to Initialize the on click event capture
function initAJAX() {
$(".controls a").click(function(e) {
e.preventDefault();
$.get($(this).attr("href"), function(data) {
var response = $('<div>' + data + '</div>');
var contents = response.find('.schedule').html()
$(".schedule").fadeOut(200, function() {
$(this).html(contents).fadeIn(200);
// After we get the content updated, we have to reinitialize those new anchors
initAJAX();
});
});
});
}
// We have to call this once to start or nothing will happen
initAJAX();
});