了解文档就绪事件

发布于 2024-09-12 00:15:01 字数 655 浏览 6 评论 0原文

嘿,我只是为我的 JS & 编写另一个基本脚本。 jQuery 实践,当我意识到这实际上包括什么时,我刚刚推出了 goold ol' document-ready 事件,我想知道我是否正确:

这是为 jQuery 准备好的文档:

$(document).ready(function(){});

现在,美元符号是jQuery的简写,它调用了jQuery库,所以我们可以进行jQuery的语句和调用,对吗?

(document) 是一个选择器,它引用 DOM 的“最高”部分(除了 Window?)。

.ready 是 DOM 完全加载时发生的操作。现在,“DOM”已完全加载,这种情况下的 DOM 是否指的是正在选择的内容?那么,如果选择了正文而不是文档,脚本会在加载之前执行吗?

(function(){});

这部分让我有点困惑。

我知道一旦我们的文档加载,它将运行我们的脚本。换句话说,它会运行我们的函数,对吗?我们的整个脚本是否被视为一个函数?而且,这实际上只是一个很大的 JavaScript 语句,对吗?因为它以分号结尾。另外,为什么我们的脚本通常放在大括号之间,而不是函数的括号之间?有什么区别?

非常感谢,抱歉提出 n00by 问题,我只是好奇! xD

Hey, I was just writing another basic script for my JS & jQuery practice, and I was just pumping out the goold ol' document-ready event when I realized what this was actually comprise of, and I wanted to know if I was right or not:

Here is the document ready for jQuery:

$(document).ready(function(){});

Now, the dollar sign is shorthand for jQuery, which calls the jQuery library, so we can make jQuery statements and calls, correct?

The (document) is a selector that is referring to the "highest" piece of the DOM (other than Window?).

.ready is an action that occurs when the DOM is fully loaded. Now, the "DOM" being fully loaded, does the DOM in this case refer to what's being selected? So if, body was selected instead of document, would the script executed before the loaded?

(function(){});

This part gets me a little confused.

I know that once our document has loaded, then it will run our script. In other words, it will run our function right? Is our whole script being thought of as a function? And, it's really just one big JavaScript statement, correct? Because it ends in a semi-colon. Also, why does our script generally go between the braces, and not the parentheses of the function? What is the difference?

Thanks so much, sorry for the n00by question, I was just curious! xD

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

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

发布评论

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

评论(7

北城半夏 2024-09-19 00:15:01

哇,这是一个答案的一大堆问题:)

现在,美元符号是 jQuery 的简写,它调用 jQuery 库,因此我们可以进行 jQuery 语句和调用,对吗?

是的,$jQuery 引用同一个对象。取自 jQuery 的源代码:

// Expose jQuery to the global object
window.jQuery = window.$ = jQuery;

window 是全局对象。添加到其中的任何内容都可以全局使用,因此您可以将其称为 window.$,或者只是 $

(文档)是一个选择器,它引用 DOM 的“最高”部分(除了 Window?)。

document 不是选择器,而是引用顶部的 DOM 对象 - DOM 中的大多数节点。它具有其他属性,例如 document.domain 等。其子元素之一是 元素。

.ready 是 DOM 完全加载时发生的操作。现在,“DOM”已完全加载,本例中的 DOM 是否指的是正在选择的内容?

是的,DOM 指的是我们通常在 jQuery 选择器中选择的项目。更具体地说,它是页面在内存中的表示。 ready 使用不同浏览器的一堆事件来确定 DOM 何时加载。

那么,如果选择了正文而不是文档,脚本会在加载之前执行吗?

目前,jQuery 的源代码并不关心调用 ready 时传入的选择器。这是准备好的函数:

ready: function( fn ) {
    // Attach the listeners
    jQuery.bindReady(); 

    // If the DOM is already ready
    if ( jQuery.isReady ) {
        // Execute the function immediately
        fn.call( document, jQuery );

    // Otherwise, remember the function for later
    } else if ( readyList ) {
        // Add the function to the wait list
        readyList.push( fn );
    }

    return this;
},

因为它不关心传入的选择器,所以您也可以传递 body ,什么也不传递,或者任何您想要的东西。

$({
    an: 'object', 
    that: 'has', 
    nothing: 'to', 
    'do': 'with', 
    ready: 'event'
}).ready(function() { .. });

它仍然有效。

(function(){});

这部分让我有点困惑。

我知道一旦我们的文档加载,它将运行我们的脚本。换句话说,它会运行我们的函数,对吗?

是的,这个函数以及您与就绪事件绑定的每个函数都将在 DOM 就绪时执行。

我们的整个脚本是否被视为一个函数?

不,不是整个脚本。仅依赖于 DOM 的项目。有些东西被发现后就需要进行处理。想想 jQuery 库本身。在处理之前它不会等待任何 DOM 就绪事件。如果您编写一条 JavaScript 语句,它将按照找到的顺序进行处理,除非它是像您传递给 ready(..) 的回调函数。因此,无论 DOM 是否加载,下面的代码都会立即执行并发出“hello”警报。

<script>
    function hello() { alert("hello"); }
    hello();
</script>

而且,这实际上只是一个很大的 JavaScript 语句,对吗?

并不真地。您可以根据需要灵活地模块化您的 JavaScript。例如,您可以拥有类似于类、对象、可重用小部件、MVC 等架构模式以及其他一些东西。

因为它以分号结尾。

分号与执行某些内容无关。我写得很好。

<script>
    alert("Hello"), alert("World")
</script>

这将起作用并按顺序提醒两个单词,并且没有分号。

另外,为什么我们的脚本通常放在大括号之间,而不是函数的括号之间?有什么区别?

这就是 JavaScript 和其他几种语言中函数的定义方式。提高你的基本技能,以便更好地理解。不要将其称为脚本,因为它只会使事情变得混乱。它只是一个函数,里面有一些语句。

Wow, that is a lof of questions for one answer :)

Now, the dollar sign is shorthand for jQuery, which calls the jQuery library, so we can make jQuery statements and calls, correct?

Yes, $ and jQuery refer to the same object. Taken from jQuery's source:

// Expose jQuery to the global object
window.jQuery = window.$ = jQuery;

window is the global object. Anything added to it becomes available globally, so you may call it either as window.$, or just $ for instance.

The (document) is a selector that is referring to the "highest" piece of the DOM (other than Window?).

document is not a selector, but a DOM object referring to the top-most node in the DOM. It has other properties such as document.domain, etc. One of its children is the <html> element.

.ready is an action that occurs when the DOM is fully loaded. Now, the "DOM" being fully loaded, does the DOM in this case refer to what's being selected?

Yes, DOM refers to the items we usually select in a jQuery selector. More specifically it is the in-memory representation of the page. ready uses a bunch of events for different browsers to determine when the DOM has loaded.

So if, body was selected instead of document, would the script executed before the loaded?

Currently jQuery's source code does not care what selector was passed in when you're calling ready. Here's the ready function:

ready: function( fn ) {
    // Attach the listeners
    jQuery.bindReady(); 

    // If the DOM is already ready
    if ( jQuery.isReady ) {
        // Execute the function immediately
        fn.call( document, jQuery );

    // Otherwise, remember the function for later
    } else if ( readyList ) {
        // Add the function to the wait list
        readyList.push( fn );
    }

    return this;
},

Since it doesn't care about what selector is passed in, you could just as well pass it body, nothing, or anything you wish.

$({
    an: 'object', 
    that: 'has', 
    nothing: 'to', 
    'do': 'with', 
    ready: 'event'
}).ready(function() { .. });

and it will still work.

(function(){});

This part gets me a little confused.

I know that once our document has loaded, then it will run our script. In other words, it will run our function right?

Yes, this and each function that you bind with the ready event will be executed when the DOM is ready.

Is our whole script being thought of as a function?

No, not the entire script. Only items that depend on the DOM. Some things need to be processed as they are found. Think about the jQuery library itself. It does not wait for any DOM ready event before getting processed. If you write a JavaScript statement, it will be processed in the order it was found unless it is a callback function like the one you pass to ready(..). So the code below will execute immediately and alert "hello" regardless of whether the DOM is loaded or not.

<script>
    function hello() { alert("hello"); }
    hello();
</script>

And, it's really just one big JavaScript statement, correct?

Not really. You can modularize your JavaScript as neatly as you want. For example, you can have something akin to classes, objects, reusable widgets, architecture patterns such as MVC among a bunch of other things.

Because it ends in a semi-colon.

A semi-colon has nothing to do with when something gets executed. I could very well write.

<script>
    alert("Hello"), alert("World")
</script>

which will work and alert the two words in sequence and there is no semi-colon.

Also, why does our script generally go between the braces, and not the parentheses of the function? What is the difference?

It's how a function is defined in JavaScript and several other languages. Brush up your basic skills to understand better. Don't refer to it as a script as it only confuses matters. It is just a function with some statements inside it.

弄潮 2024-09-19 00:15:01

.ready() 函数仅在选择文档时触发。我不相信你可以调用 $(body).ready()

基本上,ready() 函数需要 1 个参数 - 一个函数。你可以很容易地调用

$(document).ready(alert)

但是我们通常做的是用 function(){} 定义一个匿名函数

你是正确的,典型的 $(document).ready(function 中的所有代码(){}) 只是一个大函数。

我们的脚本位于大括号之间,因为这就是定义函数的方式。

我们不是这样做:而是

function myFunc()
{
  alert('We are in my function');
}
$(document).ready(myFunc)

去掉中间人,然后这样做:

$(document).ready(function()
{
  alert('We are in my function');
});

The .ready() function only fires when document is selected. I don't believe you can call $(body).ready()

Basically, the ready() function takes 1 argument - a function. You could easily call

$(document).ready(alert)

But what we usually do is define an anonymous function with function(){}

You're correct in that all the code inside the typical $(document).ready(function(){}) is just one big function.

Our script goes between the curly braces, because that's how you define a function.

Rather than doing this:

function myFunc()
{
  alert('We are in my function');
}
$(document).ready(myFunc)

we cut out the middle man and do this:

$(document).ready(function()
{
  alert('We are in my function');
});
旧梦荧光笔 2024-09-19 00:15:01
  1. (document) 不是选择器。它是对“文档”对象的引用。
  2. .ready 是对 jQuery 对象上可用函数的引用。它是 .bind("ready", f) 的简写
  3. (function() { ... }) 是事件的处理程序。

将函数绑定为“就绪”处理程序在概念上与绑定“单击”处理程序没有什么不同。

  1. (document) is not a selector. It's a reference to the "document" object.
  2. .ready is a reference to a function available on jQuery objects. It's shorthand for .bind("ready", f)
  3. (function() { ... }) is the handler for the event.

Binding a function as a "ready" handler is conceptually no different than binding a "click" handler.

屌丝范 2024-09-19 00:15:01

现在,美元符号是
jQuery,它调用 jQuery
库,所以我们可以制作 jQuery
声明和呼吁,正确吗?声明和呼吁,正确吗?

是的

.ready 是在以下情况下发生的操作
DOM 已完全加载。现在,
“DOM”已完全加载,DOM 是否已完全加载
在这种情况下指的是正在发生的事情
被选中?所以如果选择了 body
而不是文档,将是脚本
在加载之前执行?在加载之前执行?

不可以。只有文档可以触发就绪事件。

这是当 dom 准备好时将执行的函数

$(document).ready(function(){alert('ready')})

你可以写:

$(document).ready(mxFunction)
function mxFunction(){alert('ready')}

Now, the dollar sign is shorthand for
jQuery, which calls the jQuery
library, so we can make jQuery
statements and calls, correct?statements and calls, correct?

Yes

.ready is an action that occurs when
the DOM is fully loaded. Now, the
"DOM" being fully loaded, does the DOM
in this case refer to what's being
selected? So if, body was selected
instead of document, would the script
executed before the loaded?executed before the loaded?

No. Only the document can fire the ready event.

This is the function that will be execute when the dom is ready

$(document).ready(function(){alert('ready')})

You colud alos write:

$(document).ready(mxFunction)
function mxFunction(){alert('ready')}
我最亲爱的 2024-09-19 00:15:01

我不同意@eskimoblood
“只有文档才能触发就绪事件。”事实并非如此,没有文档就绪事件,“ready”是由 jQuery 和其他库引入的,用于指示 DOM 已就绪,但不一定已加载。

当所有图像帧和其他资源都已加载时, window.loaded 会被触发,因此“ready”将在“loaded”之前触发。看一下“DOMContentLoaded”,它将为您提供有关如何实现“ready”类型事件的更多详细信息。

I have to disagree with @eskimoblood
'Only the document can fire the ready event.' That is not true there is no document ready event, 'ready' is introduced by jQuery and other libraries to indicate that the DOM is ready not necessarily loaded.

window.loaded is fired when all the images frames and other assets have been loaded so 'ready' will fire before 'loaded'. Take a look at 'DOMContentLoaded' it will give you more details on how 'ready' type event is implemented.

记忆之渊 2024-09-19 00:15:01

在 jQuery 中,您可以向任何 jQuery 事件添加一个函数,以便在事件发生后运行。

在本例中,当(文档)准备就绪()时,运行其中的 function(){}

在某些情况下,您可以向函数添加参数。例如,当使用 jQuery.ajax 我可以使用参数在成功设置中,为了访问我从 ajax 请求返回的数据,并在函数本身中重用该参数。

$.ajax({
  url: "test.html",
  cache: false,
  success: function(html){
    $("#results").append(html);
  }
});

In jQuery, you're able to add a function to any jQuery event to be run once the event has happened.

In this case, when the (document) is ready(), run the function(){} within it.

In some cases you're able to add parameters to the function. For example, when using jQuery.ajax I can use parameters in the success setting, to gain access to the data I got back from the ajax request, and reuse that paramter in the function itself.

$.ajax({
  url: "test.html",
  cache: false,
  success: function(html){
    $("#results").append(html);
  }
});
走走停停 2024-09-19 00:15:01

我不太了解 JavaScript 和 jQuery,但是语句在 {} 中,参数在 () 中。那么 () 传递给函数的内容 {} 函数的作用是什么

I amm not really clued up on JavaScript and jQuery but statements go in {} and parameters go in (). So () what gets passed into function {} what function does

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