3

我一直在阅读有关此问题的几个问题,但我没有找到解决问题的方法。对不起,如果我重复之前问过的事情。

在代码中:

$("#menuZone").load("northMenu.jsp", function(response, status, xhr) {
                if (status == "error") {
                    var msg = "Error found: ";
                    alert(msg + xhr.status + " " + xhr.statusText);
                }else{
                    // hide admin menus
                    $('.admin').hide();
                }
            });

我要做的是从“northMenu.jsp”加载一个无序列表并隐藏具有 class 的项目(li 标签)admin

在上面的代码中,带有 的项目class没有被隐藏,但是完整html的被完美地插入了#menuZone.

之后,在其他地方调用函数来显示和隐藏该项目,工作正常。

jquery load doc中说,“如果提供了完整的回调,则在后处理和 HTML 插入完成后执行”

所以,问题不应该是异步问题!或者可以吗?

4

1 回答 1

2

Since this fiddle is working for you, it does not seem to be an issue with your browser. Assuming nothing in your code is tampering with load() (a reasonable assumption IMHO), that only leaves two possible explanations:

  1. $(".admin") matches nothing because the markup coming from the AJAX request does not contain class attributes, and these attributes are added by code somewhere down the line.

  2. The elements are actually hidden, but are shown again immediately afterwards by another part of the code (you mentioned other AJAX requests being performed in your question's comments).

Therefore, I'd suggest you instrument your complete callback by adding an alert() call:

// Hide admin menus.
$(".admin").hide();
alert($(".admin").length);

There are three possible outcomes:

  • The alert prints 0, which means (1) is true.
  • The alert prints a number greater than 0, in which case:
    • Without dismissing the alert box, check if the .admin elements are visible,
      • If they aren't, then (2) is true.
      • If they are, then something else I failed to think of is true.
于 2012-06-01T20:30:15.477 回答