日期:2014-05-16  浏览次数:20496 次

【jquery】jquery里的$是啥米对象
不是$()是单独的一个$.因为我在jquery.960gs里看到有function($){}的写法。我读程序到这里看不明白。下面的脚本给我加个注释就更谢谢你们了

(function ($) {
$.fn.addGrid = function (cols, options) {
var defaults = {
default_cols: 12,
z_index: 999,
img_path: '/images/',
opacity:.6
};

// Extend our default options with those provided.
var opts = $.extend(defaults, options);

var cols = cols != null && (cols === 12 || cols === 16) ? cols : 12;
var cols = cols === opts.default_cols ? '12_col' : '16_col';

return this.each(function () {
var $el = $(this);
var height = $el.height();

var wrapper = $('<div id="'+opts.grid_id+'"/>')
.appendTo($el)
.css({
'display':'none',
'position':'absolute',
'top':0,
'z-index':(opts.z_index -1),
'height':height,
'opacity':opts.opacity,
'width':'100%'});

$('<div/>')
.addClass('container_12')
.css({
'margin':'0 auto',
'width':'960px',
'height':height,
'background-image': 'url('+opts.img_path+cols + '.png)',
'background-repeat': 'repeat-y'})
.appendTo(wrapper);

// add toggle
$('<div>grid on</div>')
.appendTo($el)
.css({
'position':'absolute',
'top':0,
'left':0,
'z-index':opts.z_index,
'background': '#222',
'color':'#fff',
'padding': '3px 6px',
'width': '40px',
'text-align':'center'
})
.hover( function() {
$(this).css("cursor", "pointer");
}, function() {
$(this).css("cursor", "default");
})
.toggle( function () {
$(this).text("grid off");
$('#'+opts.grid_id).slideDown();
},
function() {
$(this).text("grid on");
$('#'+opts.grid_id).slideUp();
});
});
};
})(jQuery);

------解决方案--------------------
给jquery添加自定义方法,将jQuery作为匿名函数的参数【不传递$是防止$被重新定义过】,匿名函数$就是参数,为jquery
------解决方案--------------------
(function( window, undefined ) {
var
// A central reference to the root jQuery(document)
rootjQuery,

// The deferred used on DOM ready
readyList,

// Use the correct document accordingly with window argument (sandbox)
document = window.document,
location = window.location,
navigator = window.navigator,

// Map over jQuery in case of overwrite
_jQuery = window.jQuery,

// Map over the $ in case of overwrite
_$ = window.$,

jQuery中最开始的一段