gpt4 book ai didi

javascript - 了解文档就绪事件

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:49:36 24 4
gpt4 key购买 nike

嘿,我刚刚为我的 JS 和 jQuery 实践编写了另一个基本脚本,当我意识到这实际上是由什么组成时,我只是抽出 goold ol' 文档就绪事件,我想知道我是否对不对:

这是为 jQuery 准备的文档:

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

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

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

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

(function(){});

这部分让我有点困惑。

我知道一旦我们的文档加载完毕,它就会运行我们的脚本。换句话说,它会运行我们的功能吗?我们的整个脚本是否被认为是一个函数?而且,它真的只是一个大的 JavaScript 语句,对吗?因为它以分号结尾。另外,为什么我们的脚本通常位于大括号之间,而不是函数的括号之间?有什么区别?

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

最佳答案

哇,这么多问题一个回答:)

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

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

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

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

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

document不是选择器,而是 DOM object指的是 DOM 中最顶层的节点。它具有其他属性,例如 document.domain等。它的一个 child 是 <html>元素。

.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?

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

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

目前 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(){});

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?

是的,当 DOM 就绪时,将执行此函数以及您与 ready 事件绑定(bind)的每个函数。

Is our whole script being thought of as a function?

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

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

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

不是真的。你可以随心所欲地模块化你的 JavaScript。例如,您可以拥有类似于类、对象、可重复使用的小部件、架构模式(例如 MVC)等等。

Because it ends in a semi-colon.

分号与何时执行某事无关。我写得很好。

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

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

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

这就是在 JavaScript 和其他几种语言中定义函数的方式。复习你的基本技能,以便更好地理解。不要将其称为脚本,因为它只会混淆问题。它只是一个函数,里面有一些语句。

关于javascript - 了解文档就绪事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3364255/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com