Javascript 使用jqueryget提取ajax请求片段
我很难得到这个合作的请求。我想首先说,PHP方面是完美的。我现在尝试使用ajax和jQuery使其看起来更流畅 以下是js:Javascript 使用jqueryget提取ajax请求片段,javascript,php,jquery,ajax,get,Javascript,Php,Jquery,Ajax,Get,我很难得到这个合作的请求。我想首先说,PHP方面是完美的。我现在尝试使用ajax和jQuery使其看起来更流畅 以下是js: $(".controls a").click(function(e) { e.preventDefault(); $.get($(this).attr("href"), function(data) { $(".schedule").fadeOut(200, function() { $(this).html(dat
$(".controls a").click(function(e) {
e.preventDefault();
$.get($(this).attr("href"), function(data) {
$(".schedule").fadeOut(200, function() {
$(this).html(data).fadeIn(200);
});
});
});
$this.attrref是指为了从MySQL数据库获取信息而传递的URL,例如hours.php?week=2014-08-11。每周传递的值在每次单击链接时通过PHP更新
以下是我从console.logdata获得的信息
我尝试通过执行以下操作将数据原始HTML转换为jQuery对象:
var $data = $.parseHTML(data);
但是,这只是将HTML转换为数组。我可以对元素执行查找:
console.log($($data).find(".schedule"));
但是当我查看这个输出时,上下文是未定义的
我也尝试了这个问题的公认答案,但没有结果:
var foo = $(".schedule", $data);
console.log(foo);
这仍然有一个未定义的上下文
理想情况下,我只想获取.section中的信息,并用GET请求中捕获的信息替换当前的in.section。节是文档的一部分,也是请求返回的内容
控制台中没有错误。jQuery版本是1.11.1
如果问题写得不好,我深表歉意。我试着用一种通用的方式写它,这样它也可以适用于其他人。如果您需要更多信息,请告诉我 尝试使用jQuery筛选函数
$(".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);
});
});
});
尝试包装响应,以便可以分析它
$(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();
});
当您打开浏览器的开发工具时,您是否在控制台中看到任何错误?当前html文档或返回的数据中是否有.schedule?抱歉,请见谅。该文件中没有错误console@Sean.schedule在当前文档中,返回的数据是。schedule已在HTML中,还是包含在数据中?在js停止工作之前,它只能工作一次,不会出现错误。有什么想法吗?已编辑以解决您的问题并添加了注释。您的解决方案无效。它也有同样的行为。从字面上复制并粘贴了你的代码,在js停止工作之前,它会在8/11到8/18之间工作一次。有什么想法吗?