第二个 $(document).ready 事件 jQuery

发布于 2024-07-17 01:17:12 字数 370 浏览 4 评论 0原文

我使用一些外部 jQuery 和 $(document).ready() 在文档就绪事件触发后插入广告,如下所示:

$(document).ready( function() {
  $('#leaderboard').html("<strong>ad code</strong>");     
});

这是为了防止 UI 因广告加载缓慢而被阻止。 到目前为止,一切进展顺利。

现在我需要通过我们的 CMS 系统插入更多广告,这不能是外部 JS 文件的一部分,所以我想知道我是否可以使用第二个文档就绪事件并使用内联脚本标记插入它? 如果是这样,首先执行外部JS文档就绪事件还是先执行内联脚本的顺序是什么?

I'm using some external jQuery with $(document).ready() to insert adverts after the document ready event has fired, something like:

$(document).ready( function() {
  $('#leaderboard').html("<strong>ad code</strong>");     
});

This is to prevent the UI being blocked by the slow loading of the adverts. So far it's been working well.

Now I need to insert some more ads though our CMS system, this can't be part of the external JS file, so I'm wondering can I use a second document ready event and insert it using an inline script tag? If so, what will be the order of execution the external JS document ready event first or the inline script?

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

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

发布评论

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

评论(5

新人笑 2024-07-24 01:17:13

JQuery 按照定义的顺序调用就绪函数。
如果您想先加载一些数据并延迟执行,请使用 holdReady()

JQuery calls the ready functions in the order they are defined.
If you want to load some data first and deleay execution use holdReady().

太阳男子 2024-07-24 01:17:12

您可以根据需要使用任意多个事件方法,jquery 将它们加入队列中。 方法调用的顺序与定义顺序相同 - 最后添加的是最后调用的。

一个有用的事情可能还在于,您可以使用 ajax 使用脚本加载 html 代码,并且当代码加载到 DOM 中时 $().ready() 也会被调用,因此您可以动态加载广告。

You can use as many event methods as you want, jquery joins them in a queue. Order of method call is same as definition order - last added is last called.

A useful thing may be also that, you can load html code with script using ajax and when code is loaded into DOM $().ready() also will be called, so you can load ads dynamically.

似梦非梦 2024-07-24 01:17:12

是的,添加多个 $(documents).ready() 不是问题。 所有操作都将在就绪事件上执行。

但请注意,您的代码示例是错误的。 $(document).ready() 接受一个函数,而不是一个表达式。 所以你应该给它提供一个像这样的函数:

 $(document).ready( function() {
  $('#leaderboard').html("<strong>ad code</strong>");     
 });

当文档准备好时,该函数将被执行。

Yes, adding multiple $(documents).ready()s is not a problem. All will be executed on the ready event.

Note however that your code sample is wrong. $(document).ready() takes a function, not an expression. So you should feed it a function like this:

 $(document).ready( function() {
  $('#leaderboard').html("<strong>ad code</strong>");     
 });

That function will be executed when the document is ready.

靖瑶 2024-07-24 01:17:12

jQuery 方式的一个额外好处是
你可以有多个ready()
定义。 所有的情况都是如此
jQuery 事件。

$(文档).ready(function () {
警报(“第一名”); });

$(文档).ready(function () {
警报(“第二名”);

An added bonus of the jQuery way is
that you can have multiple ready()
definitions. This is the case with all
jQuery events.

$(document).ready(function () {
alert("Number One"); });

$(document).ready(function () {
alert("Number Two");

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