What is the difference between $ and $$?

前端 未结 7 1043
执笔经年
执笔经年 2020-12-15 09:19

I have been going through some jQuery functionality.

Can any one please give me some idea of what the difference is between the use of $ and $$

相关标签:
7条回答
  • 2020-12-15 09:41

    $ and $$ will work on any web page (if jQuery is not included also) on Google Chrome, Firefox and Safari browsers where $ returns first element of selector passed.

    Here,

    $ is document.querySelector

    $$ is document.querySelectorAll

    They are native functions of Google Chrome and Firefox browsers, you can see $ and $$ definition in Safari as well.

    Open Google in any of Google Chrome, Firefox or Safari, and open Developer Tools to check these results... (why Google, because they won't use jQuery or Moo tools)

    $('div');  // returns first DIV in DOM
    $$('div'); // returns all DIVs in DOM
    

    0 讨论(0)
  • 2020-12-15 09:45

    On jQuery documentation there is no $$ statement. jQuery has a default selector with $ character. Maybe this scripts uses another javascript lib and has some conflicts with jQuery. In this case, you can use jquery.NoConflict to avoid this kind of problem, and set another jquery selector.

    Something like:

    var s = jQuery.noConflict();
    
    // something with new jQuery selector
    s("div p").hide();
    
    // something with another library using $()
    $("content").style.display = 'none';
    

    If your code has somethig like to avoid conflicts: var $$ = jquery.noConfict();, you can use $$ as a jquery selector: $$("#element").method();

    See more on the documentation: http://api.jquery.com/jQuery.noConflict/

    0 讨论(0)
  • 2020-12-15 09:48

    $$ has no significant in Jquery, however it is used within the prototype framework.

    Prototype $$

    Also check that this is not a previous version of Jquery assigned using noConflict.

    Search the code for var $$ to find a possible assignment of an older jquery version.

    var $$ = jquery.noConfict();
    
    0 讨论(0)
  • 2020-12-15 09:55

    Short Anser: $$ is NOT defined in jQuery specifications, in addition the notation of single $( ) - sign means you encapsulate things inside bracket to a jQuery object.

    Thus, alias $ is an abbreviation to say - i am using jQuery library, where as double $$ is not defined in a standard jQuery library.

    0 讨论(0)
  • 2020-12-15 09:56

    jQuery is an object provided by jQuery. $ is another, which is just an alias to jQuery.

    $$ is not provided by jQuery. It's provided by other libraries, such as Mootools or Prototype.js.

    More importantly, $$ is also provided in the console of modern browsers as an alias to document.querySelectorAll. Except if it's overridden by another library. $ is also provided in the same way, as an alias to document.querySelector.

    See this answer for more information.

    0 讨论(0)
  • 2020-12-15 09:58

    $ AND $$ are mootools selectors and $ is also a jquery selector.

    see jquery noconflict-mode

    0 讨论(0)
提交回复
热议问题