Are $(function(){}); and $(“document”).ready(function(){}); the same?

后端 未结 2 806
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-11 19:17

I\'ve been enjoying Lynda.com\'s Jquery Essential Training, and I\'ve noticed that in the beginning the instructor uses:

    Fig. 1

    $(\"document\").read         


        
相关标签:
2条回答
  • 2020-12-11 19:48

    The default context is the document, so if you pass in some random mumbo jumbo string that does not reference an HTML node, it will be the document.

    $('fdsljkfdslj').context is document. And because the default context is the document, this means you don't have to specify it and can just feed a function to jQuery, $(function() { });

    And I think you mean $(document) instead, since specifying the string document isn't as popular, because document passes the real document object to jQuery. But again, this will be the same as passing nothing or mumbo jumbo string since we pass document literally.

    0 讨论(0)
  • 2020-12-11 19:52

    Yes, they are exactly the same, just aliases.

    From the jQuery site:

    All three of the following syntaxes are equivalent:

    $(document).ready(handler)
    $().ready(handler) (this is not recommended)
    $(handler)
    
    0 讨论(0)
提交回复
热议问题