本文介绍了在页面末尾加载jQuery吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些使用jQuery的代码,它在html中.我在页面底部包含了jQuery,所以问题是每次使用$都需要这样做.是未定义的.

I have some code that uses jQuery and it's in the html. I include jQuery at the bottom of the page so the problem is that every use of $. is undefined.

我记得在某处阅读过可以创建一个holder $函数的信息,当jQuery加载后,它将执行所有名为$的东西. (例如某种堆栈).

I remember reading somewhere that you can create a holder $ function and when the jQuery get loaded it will execute everything that called $. (like some sort of a stack).

您能告诉我该怎么做吗?像使用代码示例一样,或者如果您具有该文章链接,那将是很好的. :)

Can you please tell me how to do this? Like with a code example or if you have that article link it will be great. :)

推荐答案

不,您不能在加载jQuery之前进行大量的jQuery调用,然后期望稍后再播放所有内容.

No, you can't make a whole bunch of jQuery calls before it's loaded and then expect something later on to play back everything.

您的选择是:

  1. 在使用jQuery之前先加载它.
  2. 在页面和jQuery加载之后才使用任何jQuery调用.
  3. 创建自己的想要在jQuery加载后调用的初始化函数列表,并在加载页面时注册这些回调.然后,拥有您自己的函数,该函数遍历该初始化函数列表,并在加载jQuery以启动使用jQuery的初始化后调用所有这些函数.

但是,#3似乎是很多不必要的工作.要么预先加载jQuery,要么直到加载后才运行使用jQuery的初始化函数-更加干净和简单.

But, #3 seems like a lot of work that isn't really necessary. Either load jQuery up front or don't run your initialization functions that use jQuery until after it is loaded - either is cleaner and simpler.

请记住,在加载jQuery之后,您还需要加载所有jQuery插件,因为它们使用jQuery进行初始化/安装.

Keep in mind that you also need to load any jQuery plugins after jQuery is loaded because they use jQuery to initialize/install themselves.

这篇关于在页面末尾加载jQuery吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:47