jQuery和其他库,以及'$'的用法 [英] jQuery and other libraries, and the usage of '$'
问题描述
我有一个快速的,初学者型的问题.如果我要使用jQuery和其他框架,则以下语句是否有问题:
I have a quick, beginners-type-question. If I were to use jQuery and some other framework, would the following statement be problematic:
jQuery(document).ready(function () {
$("input[name='password']").focus(function () {
$("input[value='login']").attr("checked", "checked");
});
});
也就是说,在.ready()函数中使用'$'.应该将"$"替换为"jQuery"以避免冲突吗?
That is, the use of '$' within the .ready() function. Should '$' be replaced with 'jQuery' in order to avoid conflicts?
推荐答案
在使用多个使用$
的库时,常见的做法是使用noConflict模式并将jQuery的$
重新分配给其他内容. /p>
When using multiple libraries that make use of $
, the common practice is to use noConflict mode and reassign the $
for jQuery to something else.
var $jq = jQuery.noConflict();
$jq( function() {
$jq("input[name='password']").focus( function() {
$jq("input[value='login']").attr("checked","checked");
});
});
在插件开发中,一种常见的处理方法是将$作为参数传递给定义插件定义的函数并将该函数应用于jQuery对象.
In plugin development, a common way to handle this is to pass `$' in as a parameter to a function defining your plugin definition and applying the function to the jQuery object.
;(function($) {
...
})(jQuery);
这篇关于jQuery和其他库,以及'$'的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!