jQuery语法 - 何时使用$(美元)vs jQuery [重复]

时间:2021-07-16 01:13:35

This question already has an answer here:

这个问题在这里已有答案:

What's difference between this two?

这两者有什么区别?

$('#SPANID').html("Some Text");

jQuery('#SPANID').html("Some Text");

Is it something prototype vs jQuery?

这是jQuery的原型吗?

9 个解决方案

#1


27  

They both do the same thing. Most Libraries use $ as a shorter way to access functions within the libraries.

他们都做同样的事情。大多数库使用$作为访问库中函数的较短方式。

jQuery has many ways of accessing its library:

jQuery有很多方法可以访问它的库:

window.jQuery('#SPANID').html("Some Text");

window.$('#SPANID').html("Some Text");

jQuery('#SPANID').html("Some Text");

$('#SPANID').html("Some Text");

jQuery or window.jQuery can be used instead of $ if you were using more than one library.

如果您使用多个库,则可以使用jQuery或window.jQuery代替$。

JQuery has a function called jQuery.noConflict(); which relinquishs jQuery's control of the $ variable making $ not work with jQuery.

JQuery有一个名为jQuery.noConflict()的函数;它放弃了jQuery对$变量的控制,使得$不能与jQuery一起工作。

This would be good for using more than one library that use $.

这对于使用多个使用$的库是有好处的。

So you when you use jQuery you would do jQuery('#message').addClassName('read'); and $('#message').addClassName('read'); when using Prototype.

所以当你使用jQuery时,你会做jQuery('#message')。addClassName('read');和$('#message')。addClassName('read');使用Prototype时。

(This next bit is a little off topic but will help if you want to use $ with multiple libraries)

(这下一点有点偏离主题但如果你想使用多个库的$会有所帮助)

Although there is a way to use $ on different libraries at the same time, using anonymous functions. like so:

虽然有一种方法可以同时在不同的库上使用$,但使用匿名函数。像这样:

(function($){


})(jQuery);


(function($){


})(Prototype);

Each of the functions passes the library object, so jQuery and Prototype, as the variable $ allowing use to use it with many libraries. If you contain your code for each library within each one it will work.

每个函数都传递库对象,因此jQuery和Prototype作为变量$允许使用它与许多库一起使用。如果您在每个库中包含每个库的代码,它将起作用。

For example:

例如:

(function($){
    $(document).ready(function(){

         $('#message').addClass('read');

    });

})(jQuery);


(function($){
     document.observe("dom:loaded", function() {

         $('message').addClassName('read');
         //Or
         $$('#message').addClassName('read');

     });

})(Prototype);

#2


16  

if you are using more then one javascript library there can be conflicts if you just use the $. using jQuery will avoid those.

如果你使用的是一个以上的javascript库,那么如果你只是使用$就会有冲突。使用jQuery将避免这些。

#3


6  

It's an alias for the same thing. If you want to use jQuery on the same page as other libraries that use the $ symbol, you can use jQuery with the .noConflict() option. This will allow you to use $ for the other library and jQuery for the jQuery library.

这是同一件事的别名。如果要在与使用$符号的其他库相同的页面上使用jQuery,可以将jQuery与.noConflict()选项一起使用。这将允许您将$用于其他库,并使用jQuery用于jQuery库。

#4


3  

No difference actually, except for the fact that jQuery('SPANID') should actually be jQuery('#SPANID')

实际上没有区别,除了jQuery('SPANID')实际上应该是jQuery('#SPANID')的事实

$ is just a shorthand for jQuery

$只是jQuery的简写

EDIT: I understand that the real difference between $ and jQuery is in the possibile namespace collision with other libraries. Other than that, they work the same way.

编辑:我知道$和jQuery之间的真正区别在于与其他库的可能的命名空间冲突。除此之外,他们的工作方式相同。

#5


1  

Using the explicit "jquery" would avoid a * if you happened to also reference another library which used "$"

使用显式“jquery”可以避免冲突,如果你碰巧也引用另一个使用“$”的库

#6


0  

The difference is this: $ is often used by other libraries or JavaScript constructs, so it could be overwritten and may not do what you expect. jQuery always refers to the jQuery library, so it will do what you expect.

不同之处在于:$经常被其他库或JavaScript构造使用,因此可能会被覆盖,并且可能无法达到预期效果。 jQuery总是引用jQuery库,所以它会做你期望的。

In environments where you cannot guarantee that $ will not be overwritten by something else, using jQuery or jQuery.noConflict( ) is safer than $.

在您无法保证$不会被其他内容覆盖的环境中,使用jQuery或jQuery.noConflict()比$更安全。

#7


0  

Use jQuery when you've got another library that's already defined $. Use $ as the convenient shorthand it is when you don't (or inside of function scopes where you don't have to worry about it. :)

当你有另一个已定义$的库时使用jQuery。使用$作为方便的简写,当你不这样做时(或在函数范围内,你不必担心它。:)

Although personally, I think jq would make a nice shorthand (same number of keystrokes as $, probably a little less common).

虽然个人而言,我认为jq会有一个很好的速记(与$相同的击键次数,可能不太常见)。

#8


0  

It's not just short hand but it is made for other JavaScript libraries that might be using the $ character and to avoid confusion or conflict then using the jquery keyword is preferred.

它不仅仅是简写,而是针对可能使用$字符的其他JavaScript库,以避免混淆或冲突,然后使用jquery关键字是首选。

#9


0  

More details are here: http://api.jquery.com/ready/

更多详情请访问:http://api.jquery.com/ready/

The $ is an alias for jQuery which (jQuery) is a Javascript library. Plug-ins are usage of the library in a specific fashion which create specific use of the library and extend its functionality.

$是jQuery的别名,(jQuery)是一个Javascript库。插件是以特定方式使用库,它创建库的特定用途并扩展其功能。

Some examples of use:

一些使用示例:

"Regular" jQuery code:

“常规”jQuery代码:

   $(function(){ 
      // my jquery code here
    });

NOTE: this is an EVENT management which is to say, it executes on the DOM ready event.

注意:这是一个EVENT管理,也就是说,它在DOM ready事件上执行。

Plug-in code:

插件代码:

(function($){ 
   //theplugin code here 
})( jQuery ); 

This allows the use of the $ inside without confusion because it says "assign jQuery to the $ passed in the function($)

这允许使用$ inside而不会产生混淆,因为它说“将jQuery赋给函数中传递的$($)


$(function(){ 
  //stuff here such as jQuery code
});

as in

如在

$(function(){ 
  $("#mybutton").click(function() {
     alert("hi");)
  )};
});

Is the same as:

是相同的:

(function($) { 
  $(document).ready(function() { 
    $("#mybutton").click(function() {
       alert("hi");
    )};
  }) 
})(jQuery)

but perhaps not as good as (without the binding (jQuery) at the end), which only comes into play if you use other libraries that use the $.

但也许不如(没有最后的绑定(jQuery)),只有在使用其他使用$的库时才会发挥作用。


$ is just a character, thus you can have variables such as:

$只是一个字符,因此你可以有变量,如:

myvariable = 0; $myvariable = 0; my$variable = 0;

myvariable = 0; $ myvariable = 0;我的$ variable = 0;

Nothing special here, just using another character.

没有什么特别的,只是使用另一个角色。

I just so happens that a common use of $ is $ = jQuery; Other libraries use this character as a common shorthand (alias sort of) as well, thus the noconflict and the use of the full name (jQuery).

我碰巧经常使用$是$ = jQuery;其他库也使用此字符作为常见的简写(别名类型),因此noconflict和全名(jQuery)的使用。

#1


27  

They both do the same thing. Most Libraries use $ as a shorter way to access functions within the libraries.

他们都做同样的事情。大多数库使用$作为访问库中函数的较短方式。

jQuery has many ways of accessing its library:

jQuery有很多方法可以访问它的库:

window.jQuery('#SPANID').html("Some Text");

window.$('#SPANID').html("Some Text");

jQuery('#SPANID').html("Some Text");

$('#SPANID').html("Some Text");

jQuery or window.jQuery can be used instead of $ if you were using more than one library.

如果您使用多个库,则可以使用jQuery或window.jQuery代替$。

JQuery has a function called jQuery.noConflict(); which relinquishs jQuery's control of the $ variable making $ not work with jQuery.

JQuery有一个名为jQuery.noConflict()的函数;它放弃了jQuery对$变量的控制,使得$不能与jQuery一起工作。

This would be good for using more than one library that use $.

这对于使用多个使用$的库是有好处的。

So you when you use jQuery you would do jQuery('#message').addClassName('read'); and $('#message').addClassName('read'); when using Prototype.

所以当你使用jQuery时,你会做jQuery('#message')。addClassName('read');和$('#message')。addClassName('read');使用Prototype时。

(This next bit is a little off topic but will help if you want to use $ with multiple libraries)

(这下一点有点偏离主题但如果你想使用多个库的$会有所帮助)

Although there is a way to use $ on different libraries at the same time, using anonymous functions. like so:

虽然有一种方法可以同时在不同的库上使用$,但使用匿名函数。像这样:

(function($){


})(jQuery);


(function($){


})(Prototype);

Each of the functions passes the library object, so jQuery and Prototype, as the variable $ allowing use to use it with many libraries. If you contain your code for each library within each one it will work.

每个函数都传递库对象,因此jQuery和Prototype作为变量$允许使用它与许多库一起使用。如果您在每个库中包含每个库的代码,它将起作用。

For example:

例如:

(function($){
    $(document).ready(function(){

         $('#message').addClass('read');

    });

})(jQuery);


(function($){
     document.observe("dom:loaded", function() {

         $('message').addClassName('read');
         //Or
         $$('#message').addClassName('read');

     });

})(Prototype);

#2


16  

if you are using more then one javascript library there can be conflicts if you just use the $. using jQuery will avoid those.

如果你使用的是一个以上的javascript库,那么如果你只是使用$就会有冲突。使用jQuery将避免这些。

#3


6  

It's an alias for the same thing. If you want to use jQuery on the same page as other libraries that use the $ symbol, you can use jQuery with the .noConflict() option. This will allow you to use $ for the other library and jQuery for the jQuery library.

这是同一件事的别名。如果要在与使用$符号的其他库相同的页面上使用jQuery,可以将jQuery与.noConflict()选项一起使用。这将允许您将$用于其他库,并使用jQuery用于jQuery库。

#4


3  

No difference actually, except for the fact that jQuery('SPANID') should actually be jQuery('#SPANID')

实际上没有区别,除了jQuery('SPANID')实际上应该是jQuery('#SPANID')的事实

$ is just a shorthand for jQuery

$只是jQuery的简写

EDIT: I understand that the real difference between $ and jQuery is in the possibile namespace collision with other libraries. Other than that, they work the same way.

编辑:我知道$和jQuery之间的真正区别在于与其他库的可能的命名空间冲突。除此之外,他们的工作方式相同。

#5


1  

Using the explicit "jquery" would avoid a * if you happened to also reference another library which used "$"

使用显式“jquery”可以避免冲突,如果你碰巧也引用另一个使用“$”的库

#6


0  

The difference is this: $ is often used by other libraries or JavaScript constructs, so it could be overwritten and may not do what you expect. jQuery always refers to the jQuery library, so it will do what you expect.

不同之处在于:$经常被其他库或JavaScript构造使用,因此可能会被覆盖,并且可能无法达到预期效果。 jQuery总是引用jQuery库,所以它会做你期望的。

In environments where you cannot guarantee that $ will not be overwritten by something else, using jQuery or jQuery.noConflict( ) is safer than $.

在您无法保证$不会被其他内容覆盖的环境中,使用jQuery或jQuery.noConflict()比$更安全。

#7


0  

Use jQuery when you've got another library that's already defined $. Use $ as the convenient shorthand it is when you don't (or inside of function scopes where you don't have to worry about it. :)

当你有另一个已定义$的库时使用jQuery。使用$作为方便的简写,当你不这样做时(或在函数范围内,你不必担心它。:)

Although personally, I think jq would make a nice shorthand (same number of keystrokes as $, probably a little less common).

虽然个人而言,我认为jq会有一个很好的速记(与$相同的击键次数,可能不太常见)。

#8


0  

It's not just short hand but it is made for other JavaScript libraries that might be using the $ character and to avoid confusion or conflict then using the jquery keyword is preferred.

它不仅仅是简写,而是针对可能使用$字符的其他JavaScript库,以避免混淆或冲突,然后使用jquery关键字是首选。

#9


0  

More details are here: http://api.jquery.com/ready/

更多详情请访问:http://api.jquery.com/ready/

The $ is an alias for jQuery which (jQuery) is a Javascript library. Plug-ins are usage of the library in a specific fashion which create specific use of the library and extend its functionality.

$是jQuery的别名,(jQuery)是一个Javascript库。插件是以特定方式使用库,它创建库的特定用途并扩展其功能。

Some examples of use:

一些使用示例:

"Regular" jQuery code:

“常规”jQuery代码:

   $(function(){ 
      // my jquery code here
    });

NOTE: this is an EVENT management which is to say, it executes on the DOM ready event.

注意:这是一个EVENT管理,也就是说,它在DOM ready事件上执行。

Plug-in code:

插件代码:

(function($){ 
   //theplugin code here 
})( jQuery ); 

This allows the use of the $ inside without confusion because it says "assign jQuery to the $ passed in the function($)

这允许使用$ inside而不会产生混淆,因为它说“将jQuery赋给函数中传递的$($)


$(function(){ 
  //stuff here such as jQuery code
});

as in

如在

$(function(){ 
  $("#mybutton").click(function() {
     alert("hi");)
  )};
});

Is the same as:

是相同的:

(function($) { 
  $(document).ready(function() { 
    $("#mybutton").click(function() {
       alert("hi");
    )};
  }) 
})(jQuery)

but perhaps not as good as (without the binding (jQuery) at the end), which only comes into play if you use other libraries that use the $.

但也许不如(没有最后的绑定(jQuery)),只有在使用其他使用$的库时才会发挥作用。


$ is just a character, thus you can have variables such as:

$只是一个字符,因此你可以有变量,如:

myvariable = 0; $myvariable = 0; my$variable = 0;

myvariable = 0; $ myvariable = 0;我的$ variable = 0;

Nothing special here, just using another character.

没有什么特别的,只是使用另一个角色。

I just so happens that a common use of $ is $ = jQuery; Other libraries use this character as a common shorthand (alias sort of) as well, thus the noconflict and the use of the full name (jQuery).

我碰巧经常使用$是$ = jQuery;其他库也使用此字符作为常见的简写(别名类型),因此noconflict和全名(jQuery)的使用。