what is the difference between jQuery(function($) and $(function()?

偶尔善良 提交于 2020-01-24 12:47:06

问题


I'm looking at a piece of code I did not write which contains:

jQuery(function($) {

$('#interaction').find('.item').hover(function() {
    var $this = $(this);
    $this.addClass('hover');
},
function() {
    var $this = $(this);
    $this.removeClass('hover');
})
.click(function() {
    var $this = $(this);
    var thisID = $this.attr('id');
    //hide all visiable detail pages
    resetpage($('.item-detail:visible'));

... etc.

Normally I would write my code to run inside of $(document).ready({ ... }); for example:

$(document).ready({

    .click(function() {
        var $this = $(this);
        var thisID = $this.attr('id');
        //hide all visiable detail pages
        resetpage($('.item-detail:visible'));
        ... etc.

    }
});

What is the difference (if any) between these two ways of writing the function or can I use them interchangeably?


回答1:


You can use them interchangeably. $ is shorthand for jQuery, and $(function(){..}) is shorthand for $(document).ready(function(){ });

Sometimes people use jQuery(function($){ }); because the $ symbol is used by another library, or conflicts with PHP on the server.



来源:https://stackoverflow.com/questions/11461478/what-is-the-difference-between-jqueryfunction-and-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!