jQuery(函数($)和$(函数())有什么区别? [英] what is the difference between jQuery(function($) and $(function()?

查看:115
本文介绍了jQuery(函数($)和$(函数())有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  jQuery(function($){ 
$ b $('#interaction')。find('。item')。hover(function(){
var $ this = $(this);
$ this。 addClass('hover');
},
function(){
var $ this = $(this);
$ this.removeClass('hover');
$ b .click(function(){
var $ this = $(this);
var thisID = $ this.attr('id');
/ /隐藏所有可见的详细页面
resetpage($('。item-detail:visible'));

...等等

通常我会写代码在$(document).ready({...})内部运行;例如:

  $(document).ready({

.click(function(){
var $ this = $ (this);
var thisID = $ this.attr('id');
//隐藏所有可见的详细页面
resetpage($('。item-detail:visible')) ;
...等等

}
});

这两种写作方式有什么不同(如果有的话)函数还是可以互换使用的?

解决方案

您可以互换使用它们。 $ 简写为 jQuery $(function(){..}) $(document).ready(function(){});



有时候人们使用 jQuery(function($){}); 因为 $ 符号被另一个库使用,或与服务器上的 PHP 冲突。


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?

解决方案

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.

这篇关于jQuery(函数($)和$(函数())有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆