代码之家  ›  专栏  ›  技术社区  ›  bobber205

Jquery Hide()和Show()不起作用-非常Frustated

  •  2
  • bobber205  · 技术社区  · 14 年前

    我有一些这样的HTML

    <div id="topContainer">
     <div id="level1" style="display:none;"> </div>
     <div id="level2" style="display:none;"></div>
    </div>
    

    我可以检索level1和level2,成功地调用它们的show()和hide()。但是,让style=“display:none;”然后调用jQuery(“#topcainer”).show()执行nada。:(

    JS以下

    //LOGIC HERE THAT SHOWS LEVEL1 and LEVEL2 based on business logic
    
    //If neither div is shown (got a variable set to false, it set to true each time
    //the business logic shows the div
    //if variable is still false, then the below line runs
    jQuery("#topContainer").hide()
    

    更新了尽可能多的代码。

    3 回复  |  直到 14 年前
        1
  •  6
  •   Nick Craver    14 年前

    .show() .hide() 在父母身上不影响孩子,如果他们被隐藏,他们会被隐藏……他们是独立处理的。

    不过,你可以打电话给 如果需要的话,在孩子们身上,例如:

    jQuery("#topContainer").show().children().show();
    
        2
  •  1
  •   Mottie    14 年前

    我想知道隐藏 #topContainer 是造成问题的原因。我在这里设置了一个演示来显示 :empty

    你提供的HTML。我建立了一个 demo here ... 在div中添加内容,然后重新运行脚本,您可以看到区别。

    var container = $('#topContainer'),
        divs = container.find('div'),
        empty = divs.filter(':empty');
    
    if (divs.length == empty.length) {
        // hide container!
        $('#topContainer').hide();
        alert('hidden!');
    } else {
        // don't do anything
        alert("don't hide!");
    }
    
        3
  •  0
  •   mpdonadio    14 年前

    $("#topContainer").css("display", "block"); 工作?

    $("#topContainer").css("background", "red"); 工作?

    你有没有用Firebug或者其他什么东西在规则级联之前和之后显示实际的DOM属性。

    #topContainer 正在获取 display

        4
  •  0
  •   Arun Prasad E S    5 年前

    我的问题不一样,但情况有点不同

    脚本是在DOM创建要隐藏的元素之前放置的。

    我将脚本改为“页面加载后”

    $(function(){
       // logic
    });