问题
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