Jquery Hide() 和 Show() 不工作——非常沮丧
我有一些像这样的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
.show()
和.hide()
父级不会影响子级,如果它们被隐藏,它们将保持隐藏状态...它们是独立处理的。但是,您也可以对子级调用
.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:我想知道隐藏
#topContainer
的逻辑是否导致了问题。我在这里设置了一个演示来展示:empty
选择器;但为了使元素被视为空,它甚至不能包含单个空格(文本节点)。使用您提供的 HTML。我已经在此处设置了一个 演示...在 div 中添加内容,然后重新运行脚本,您可以看到差异。
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.
$("#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 gettingdisplay
set via something else, or another there is another div with #topContainer` further down the page (which would be invalid HTML).我的问题不同,情况略有不同,尽管
脚本是在 DOM 创建要隐藏的元素之前放置的。
我将脚本更改为“页面加载后”
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"