简体   繁体   English

如何在jQuery内部函数中正确使用$

[英]How to properly use $ for jQuery inside functions

I use the following ocde so I can use the $ short hand for jQuery: 我使用以下代码,以便可以将$简写用于jQuery:

jQuery(function($){ ... }

But I also have a few functions outside it that require jQuery. 但是我外面还有一些需要jQuery的函数。 How can I still use the $ short hand within those functions? 在这些功能中,我仍如何使用$简写? It seems like $ won't work within my functions. 好像$在我的函数中不起作用。

You cannot use $ outside of that function if you have another script on the page that uses $ as their object. 您不能使用$那之外function ,如果你有一个使用页面上的其他脚本$为他们的目的。 However, you can use noConflict to create an alias of $ or jQuery . 但是,您可以使用noConflict创建$jQuery的别名。

(function($) {
    // Use $ here
})(jQuery);


// Create an alias of jQuery
var $_ = jQuery.noConflict();

// Use $_ here

Remove the jQuery on the front and put after that: 删除前面的jQuery ,然后放在后面:

(function($){
  ///
})(jQuery); // Use jQuery here

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM