Javascript 有什么方法可以在 Jquery 中调用函数“在文档准备好之前”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7186914/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:15:32  来源:igfitidea点击:

Is there any way to call function 'before document ready' in Jquery?

javascriptjquery

提问by user901886

I want to call function before document get ready, so is there any method in Jquery to do this?

我想在文档准备好之前调用函数,那么 Jquery 中是否有任何方法可以做到这一点?

回答by pimvdb

If you simply call a function in the head tag it will execute immediately, before the DOM is even parsed (that's the 'problem' that readysolves).

如果你只是在 head 标签中调用一个函数,它会立即执行,甚至在 DOM 被解析之前(这就是ready解决的“问题” )。

<!doctype html>
<html>
    <head>
        <script>alert(1)</script>
    </head>
    <body>
    </body>
</html>

回答by Scott

Just call your function before the document ready statement.

只需在文档就绪语句之前调用您的函数。

<script>
   thisWillFireImmediately();

   $(function() {
      thisWillFireOnDocumentReady();
   });
</script>

回答by leenbean16

If you are using .hide() or display: none; - the hiding of the divs will be displayed.

如果您使用 .hide() 或 display: none; - 将显示隐藏的 div。

try:

尝试:

visibility: hidden;

it worked for me after trying many other ways.

在尝试了许多其他方法后,它对我有用。