jQuery $(文档).ready();声明其中的所有函数

发布于 2024-08-10 02:35:58 字数 381 浏览 5 评论 0原文

说明: 我有几个对象,我在 $(document).ready() 中声明它们。为什么?因为在数千个对象中我有很多jquery方法$(..),显然它们也可以在外面工作,但是当我包含mootool时,它就停止工作了。我尝试了 noConflict 和其他一些东西,只有当我将 $() 更改为 jQuery() 或 $j() 时,没有任何效果。并且我不想更改我的 20 个文件以及每个文件超过 2000 行。无论如何,在 $(document).ready() 中声明我的对象。让他们工作得很好。

现在我的问题是: 如果我在 $(document).ready() 方法中声明所有这些对象,它会让我的网站变慢吗?或者它会使客户端的速度变慢?这是我心中唯一关心的问题。

Explanation:
i have few objects and im declaring them inside $(document).ready(). WHY? because in thous objects i have many jquery methods $(..), obviously they can work outside too, but when i include mootool, then it stop working. i tried noConflict and some other things, nothing works, only if i change the $() to jQuery() or to $j().. and i dont want to change for my 20 files and more then 2000 lines for each file. anyway declaring my objects inside $(document).ready(). made them work just fine.

Now My Question is:
if i declare all these objects inside the $(document).ready() method, would it make my site slow? or would it make things slow from client side? thats the only concern in my mind.

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

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

发布评论

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

评论(3

没有伤那来痛 2024-08-17 02:35:58

我不明白这样做会让你的网站变慢。通过在 $().ready 中声明它们,您只是将声明的范围限制为特定的 $().ready 函数,因此它们不会在同一页面上的其他就绪函数范围内可用 - 如果您的应用程序设计良好并且您坚持每页一个,那么这实际上不会成为一个麻烦。

哦,在 DOM 完全加载之前,您的声明肯定不会被解析(如您所知, $().ready 仅在 DOM 加载后执行),但这也应该这不是问题,因为您只在准备好的函数中使用它们(至少我希望如此)。

你真的需要两个库吗?如果您只是使用这些库之一中的一两个小功能,那么您很可能可以使用您最常使用的库来模仿该行为。如果您可以/可行地做到这一点,那么您的生活就会变得更加简单。

I don't see how doing that would make your site slow. By declaring them within the $().ready you're simply restricting the scope of your declarations to that particular $().ready function, thus they won't be available from within the scopes of other ready functions on the same page - which should not really be a bother if your application is well-designed and you've stuck to one per page.

Oh, and your declarations certainly won't have been been parsed until the DOM is fully loaded, (as you know, $().ready only executes once the DOM has loaded), but that too should not be a problem as you're only utilizing them from within a ready function (at least I hope).

Do you really need two libraries? If it's just one or two little tidbits of functionality you are using from one of those libraries chances are you can mimic that behaviour using the one you're making the greatest use of. If you can possibly/feasibly do that it will make your life so much simpler.

陌上芳菲 2024-08-17 02:35:58

jQuery.ready 中执行所有操作都不会减慢您的网站速度。

作为替代解决方案,您可以在所有 jQuery 代码中将 $ 替换为 jQuery,或者您可以将其包装在如下函数中:

(function($) {
    $('whatever').something();
})(jQuery);

此代码创建一个函数,该函数采用一个名为 $ 的参数,并使用 jQuery 对象调用该函数。 $ 参数将在函数范围内隐藏 mootools 的全局 $ 对象,允许您在函数内编写普通的 jQuery 代码。

Doing everything in jQuery.ready will not slow down your site.

As an alternative solution, you could replace $ with jQuery in all of your jQuery code, or you could wrap it in a function like this:

(function($) {
    $('whatever').something();
})(jQuery);

This code makes a function that takes a paremeter called $, and calls that function with the jQuery object. The $ parameter will hide mootools' global $ object within the scope of the function, allowing you to write normal jQuery code inside the function.

野却迷人 2024-08-17 02:35:58

只需声明
document.ready 请求之前的 jQuery.noConflict,然后将 jQuery 方法别名为 document.ready 中的 $...

jQuery.noConflict();
jQuery(document).ready(function($){

});

Just declare
jQuery.noConflict before the document.ready request, then alias the jQuery method to $ within in the document.ready...

jQuery.noConflict();
jQuery(document).ready(function($){

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