javascript 使用 jQuery UI Accordion 小部件在页面加载时隐藏 div
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5923836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hiding divs on page load with jQuery UI Accordion widget
提问by Yazmin
I'm using jQuery Accordion. When the page loads, all the divs flash for a second before being hidden. I'd like the divs to stay hidden while loading. I thought I was doing that by setting them to hidden via javascript outside of the document ready check like so:
我正在使用 jQuery 手风琴。当页面加载时,所有 div 在隐藏之前都会闪烁一秒钟。我希望 div 在加载时保持隐藏状态。我以为我是通过在文档就绪检查之外通过 javascript 将它们设置为隐藏来做到这一点的,如下所示:
$('#accordion div').hide();
$('#accordion2 div').hide();
jQuery(document).ready(function($) {...
However this isn't working and I suspect it's because I'm using the $ shortcut not yet declared.
但是,这不起作用,我怀疑这是因为我正在使用尚未声明的 $ 快捷方式。
How do I get the hide() functions to fire while the page is loading instead of waiting until it is fully loaded and then hiding the divs?
如何在页面加载时触发 hide() 函数,而不是等到它完全加载然后隐藏 div?
Thanks!
谢谢!
回答by Guffa
The reason that it's not working is because you are trying to hide the elements before they exist. It would work if you put the code at the end of the page, but even then you are not sure that they are not visible for a split second.
它不起作用的原因是因为您试图在元素存在之前隐藏它们。如果您将代码放在页面的末尾,它会起作用,但即使如此,您也不确定它们是否在一瞬间不可见。
Use CSS to hide them instead, then they are hidden already when they come into existance:
使用 CSS 来隐藏它们,然后当它们出现时它们就已经隐藏了:
<style type="text/css">
#accordion div, #accordion2 div { display: none; }
</style>
回答by bitfox
These statements try to hide something that has not been loaded yet:
这些语句试图隐藏尚未加载的内容:
$('#accordion div').hide();
$('#accordion2 div').hide();
With jQuery you should write this:
使用 jQuery 你应该这样写:
$(function(){
$('#accordion div').hide();
$('#accordion2 div').hide();
});
So, the code will be executed after the skeleton of html document will be loaded
所以,代码将在加载html文档的骨架后执行