简体   繁体   English

$(文件).ready之前的事件

[英]Events before $(document).ready

I have a functionality to hide controls conditionally before the document is loaded. 我有一个功能,可以在加载文档之前有条件地隐藏控件。 If I put this functionality on $(document).ready , I see the page flickering to hide the controls conditionally. 如果我将此功能放在$(document).ready ,我会看到页面闪烁以有条件地隐藏控件。 I would like to know if there is an event that can be called before $(document).ready is triggered. 我想知道在$(document).ready被触发之前是否有可以调用的事件。

Common issue. 常见问题。 Use CSS to have the controls hidden by default and then use JS upon $(document).ready to decide which controls to make visible. 使用CSS默认隐藏控件,然后在$(document).ready上使用JS来决定使哪些控件可见。 There will be no flicker. 没有闪烁。 It will just look like the appropriate parts of the page are loading progressively. 看起来页面的相应部分正在逐步加载。

You can't safely run JS before the document is ready and parts of the document will be visible before you can run JS. 在文档准备好之前,您无法安全地运行JS,并且在运行JS之前,部分文档将可见。 So, the only solution is to make sure all non-flicker elements are hidden by default and you then only show the ones you want visible. 因此,唯一的解决方案是确保默认情况下隐藏所有非闪烁元素,然后只显示您想要显示的元素。

The simplest way to do this is to just put a common class on all dynamic elements: 最简单的方法是在所有动态元素上放置一个公共类:

<div id="myControl1" class="initiallyHidden"></div>

and use CSS to make sure they all start out hidden: 并使用CSS确保它们全部隐藏起来:

.initiallyHidden {display: none;}

And then your javascript will override that when it decides an element should be visible: 然后你的javascript将在它决定一个元素应该可见时覆盖它:

document.getElementById("myControl1").style.display = "block";

As others mentioned do something like this 正如其他人提到的那样做

<div id="test" class="hidden"> my hidden div tag </div>

.hidden
{
 display:none;   
}

in the document.ready , you can show , this is equivalent of onload , which waits till html is loaded 在document.ready中,您可以显示,这相当于onload,它等待加载html

$(document).ready(function() {
  $('#test').show();
});

jsfiddle example here jsfiddle这里的例子

http://jsfiddle.net/kdpAr/ http://jsfiddle.net/kdpAr/

When I've needed to hide something before Javascript is loaded I would set it hidden in css like so: 当我在加载Javascript之前需要隐藏某些内容时,我会将其隐藏在css中,如下所示:

 display:none;

or: 要么:

 visibility: hidden;

You can also use conditions in combination with Javascript to reveal it if need be. 如果需要,您还可以将条件与Javascript结合使用以显示它。

call the JavaScript function in next to HTML element 在HTML元素旁边调用JavaScript函数

ex: 例如:

<span></span>
<script> call JS function</script>

You can just open a script tag right after the HTML you want to hide condionnaly (jQuery must be declared before however) and then call hide directly, not inside a $(document).ready like this : 您可以在要隐藏condionnaly的HTML之后立即打开一个脚本标记(但是必须在之前声明jQuery),然后直接调用hide,而不是在$(document)中。就像这样:

<div id="toHide">Lorem ipsum</div>

<script type="text/javascript">
  $("#toHide").hide();
</script>

You have to put your elements BEFORE running your javascript 你必须在运行你的javascript之前放置你的元素

<div>hi</div>
<script>
$("div").css('background-color', '#EEE');
</script>

http://sandbox.phpcode.eu/g/d01a1 http://sandbox.phpcode.eu/g/d01a1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM