jQuery document ready function

后端 未结 7 1174
南旧
南旧 2020-12-03 17:20

Are the end results of the following jQuery snippets identical?

Snippet 1:

$(function() { alert(\'test!\'); });

相关标签:
7条回答
  • 2020-12-03 17:53

    $(function(){}) and $(document).ready(function() { }) are identical.

    0 讨论(0)
  • 2020-12-03 17:59

    Different ways to write jQuery Document Ready Snippet

    $(document).ready(function() { ... }); 
    $(function() { ... });
    jQuery(document).ready(function() { ... });
    jQuery(function() { ... });
    

    http://webiwip.com/interview-questions-answers/jquery-interview-questions/10510

    0 讨论(0)
  • 2020-12-03 18:02

    Yes they're equivalent. See this link for reference .ready()

    0 讨论(0)
  • 2020-12-03 18:02

    The following code also working

    $(document).ready(function(){
        alert("success");
    });
    

    OR

    $(function(){
        alert("succes");
    });
    
    0 讨论(0)
  • 2020-12-03 18:07

    All three of the following syntaxes are equivalent:

    $(document).ready(handler)
    $().ready(handler) (this is not recommended)
    $(handler)
    

    Aliasing the jQuery Namespace

    When using another JavaScript library, we may wish to call $.noConflict() to avoid namespace difficulties. When this function is called, the $ shortcut is no longer available, forcing us to write jQuery each time we would normally write $. However, the handler passed to the .ready() method can take an argument, which is passed the global jQuery object. This means we can rename the object within the context of our .ready() handler without affecting other code:

    jQuery(document).ready(function($) {
      // Code using $ as usual goes here.
    });
    
    0 讨论(0)
  • 2020-12-03 18:08

    Yes, this is identical. but the first one is usually used in jquery for easiness.

    $(function() { 
        alert('test!'); 
    });
    
    0 讨论(0)
提交回复
热议问题