What does the jQuery() function in jQuery do?

后端 未结 4 611
臣服心动
臣服心动 2021-01-25 08:06

In this video there is a snippet of code that goes something like this:

if (jQuery) {jQuery(function() {
    // ...
})}

I\'ve never seen the

相关标签:
4条回答
  • 2021-01-25 08:39
    jQuery(function()
    

    is same as

    $(document).ready(function()
    
    if(jQuery)
    

    is a check whether the jQuery.js file has been loaded or not.

    There is another way to check this

    if (typeof jQuery == 'undefined')
    {
        //jQuery has not been loaded  
    }
    
    0 讨论(0)
  • 2021-01-25 08:40

    $() is an alias for jQuery(), defined as:

    // Expose jQuery to the global object
    window.jQuery = window.$ = jQuery;
    

    http://code.jquery.com/jquery-1.4.js

    there is a special case defined when $() or jQuery() is called with the first argument being a function:

    // HANDLE: $(function)
    // Shortcut for document ready
    } else if ( jQuery.isFunction( selector ) ) {
        return rootjQuery.ready( selector );
    }
    

    sometimes $ can conflict with other libraries (like prototype) that define the same function, so if you call

    jQuery.noConflict();
    

    it will remove the $ alias, setting it back to the original value found, essentially:

    window.$ = _$;
    
    0 讨论(0)
  • 2021-01-25 08:44

    I think it is the same that using $() but you use jQuery() for compatibility with other libs which also use $()

    jQuery can be a variable that store a function. Guess that if is to check if it is not undefined or something like that

    0 讨论(0)
  • 2021-01-25 08:52

    The $ function is an alias for the jQuery function. So, they are the same.

    If you use jQuery in noConflict mode, there is only jQuery() function

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