Jquery Hide() 和 Show() 不工作——非常沮丧

发布于 2024-09-30 08:40:29 字数 689 浏览 3 评论 0原文

我有一些像这样的 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("#topContainer").show() 也没什么作用。 :(

可能出了什么问题?

下面的 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()

用尽可能多的代码进行了更新。

I have some HTML like this

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

I can retrieve level1 and level2, calling show() and hide() on them successfully. However, having style="display:none;" and then calling jQuery("#topContainer").show() does nada. :(

What could possibly be wrong?

JS Below

//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()

Updated with as much code as I can.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

柏林苍穹下 2024-10-07 08:40:29

.show().hide() 父级不会影响子级,如果它们被隐藏,它们将保持隐藏状态...它们是独立处理的。

但是,您也可以对子级调用 .show() 如果需要,例如:

jQuery("#topContainer").show().children().show();

.show() and.hide() on a parent doesn't affect the children, if they're hidden they stay hidden...they're handled independently.

However, you can call .show() on the children as well if needed, for example:

jQuery("#topContainer").show().children().show();
烟若柳尘 2024-10-07 08:40:29

我想知道隐藏 #topContainer 的逻辑是否导致了问题。我在这里设置了一个演示来展示 :empty 选择器;但为了使元素被视为空,它甚至不能包含单个空格(文本节点)。

使用您提供的 HTML。我已经在此处设置了一个 演示...在 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!");
}

I wonder if the logic to hide the #topContainer is causing the problems. I set up a demo here to display the utility of the :empty selector; but in order for an element to be considered empty, it can not even contain even a single space (text node).

With the HTML you provided. I've set up a demo here... add content in the div then re-run the script, and you can see the difference.

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!");
}
☆獨立☆ 2024-10-07 08:40:29

$("#topContainer").css("display", "block"); 有效吗?

$("#topContainer").css("background", "red"); 有效吗?

您是否使用过 Firebug 或其他可以显示前后实际 DOM 属性以及规则级联的东西。

我怀疑 #topContainer 正在通过其他方式设置 display,或者页面下方还有另一个带有 #topContainer` 的 div(这将是无效的 HTML) 。

Does $("#topContainer").css("display", "block"); work?

Does $("#topContainer").css("background", "red"); work?

Have you poked around with Firebug or something else that can show the actual DOM properties before and after, along with the rule cascade.

My suspicion would be that #topContainer is getting display set via something else, or another there is another div with #topContainer` further down the page (which would be invalid HTML).

亚希 2024-10-07 08:40:29

我的问题不同,情况略有不同,尽管

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

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

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

My issue was different, situation a little different though

The script was placed before the DOM created the element to hide.

I change the script to the "after page load"

$(function(){
   // logic
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文