JQuery load() 状态没有意义

JQuery load() status is not making sense

所以我有以下代码:

$("#wifi").load("myurl .learn-press-course-results-progress", function(response, status, xhr) {
        if (status == "success") {
            $('#wifi_header').show();
            console.log("wifi");
        };
});

这基本上是试图获取学生在给定的 learnpress 课程中的进步,并且如果有任何进步(即如果学生注册了该课程),它会显示该课程 header (以及它的进步)在他们的仪表板上,这样他们就可以轻松地继续他们的课程,就像这样:

但是,和status条件的原因,如果学生没有被录取,那么div .learn-press-course-results-progress就不存在了,所以我不想显示课程标题(即,我希望“什么都不发生”。)

问题是,即使成功是有条件的,也会发生这种情况:

现在有趣的是,我嵌套了 console.log("class") 以查看发生了什么,结果证明在每个实例中,包括加载失败的实例,因此它不应该显示header,我在我的控制台中收到 class 日志。

所以,我的问题是,我该如何解决这个问题?跟状态有关系吗?

编辑:在此代码块之前,我已经完成了 $("#class_header").hide(); 这就是我 运行 show().

的原因

谢谢!

When this method executes, it retrieves the content of ajax/test.html, but then jQuery parses the returned document to find the element with an ID of container. This element, along with its contents, is inserted into the element with an ID of result, and the rest of the retrieved document is discarded.

jQuery uses the browser's .innerHTML property to parse the retrieved document and insert it into the current document. During this process, browsers often filter elements from the document such as <html>, <title>, or <head> elements. As a result, the elements retrieved by .load() may not be exactly the same as if the document were retrieved directly by the browser.

如果您致电:

$("#wifi").load("myurl .learn-press-course-results-progress");

如果页面加载,并且 none 个元素具有 .learn-press-course-results-progresswifi 将填充空数据或“”。状态还是200或success,因为myurl存在,可以调用,只是你要找的内容不见了。

这就是为什么您的 $("#wifi") 为空以及触发条件语句的原因。

考虑以下因素。

$("#wifi").load("myurl .learn-press-course-results-progress", function(response, status, xhr) {
  if ($("#wifi").children().length && status == "success") {
    $('#wifi_header').show();
    console.log("wifi");
  };
});

这会检查 wifi 中是否有任何元素。如果带回的数据只是文本,则检查文本长度。

if($("#wifi").text().trim() != "" ...){}

其中一个应该对您有所帮助。