我正在做我自己的畫廊腳本,但是當我嘗試和alert(this)
它返回[對象對象]。自定義jQuery插件返回[對象對象]
我這樣調用
$('#images').createGallery({
server: 'http://localhost/jQuery%20Gallery/images/galleries/',
galleryName: 'Test',
galleryWidth: 800,
galleryImageMargin: 20,
galleryImageColumns: 2,
galleryTargetFolder: 'homepage_gallery',
imageQuality: 100
});
和我的函數的代碼我的功能是這樣的:
(function ($) {
$.fn.createGallery = function(options) {
alert(this);
var settings = $.extend({
// These are the defaults.
server: 'http://localhost/jQuery%20Gallery/images/galleries/',
galleryName: 'Test',
galleryWidth: 800,
galleryImageMargin: 20,
galleryImageColumns: 2,
galleryTargetFolder: 'homepage_gallery',
imageQuality: 100
}, options);
var galleryImageWidth = settings.galleryWidth/settings.galleryImageColumns;
var imageUrl = settings.server+settings.galleryTargetFolder;
var otherMargin = Math.round(settings.galleryImageMargin/2);
var finalImageWidth = Math.round(galleryImageWidth - settings.galleryImageMargin);
var finalImageHeight = Math.round(galleryImageWidth/1.4);
var finalGalleryWidth = settings.galleryWidth - settings.galleryImageMargin;
$('#'+this).before('<style>#'+this+' li:nth-child('+settings.galleryImageColumns+'n+1) { margin-left: 0; } #'+this+' li:first-child { margin-left: 0; } #'+this+' { width: '+finalGalleryWidth+'px; margin: 0px; } #'+this+' li { display: inline-block; list-style: none; margin-left: '+settings.galleryImageMargin+'px; margin-bottom: '+otherMargin+'px; } </style>');
$.ajax({
url: imageUrl,
success: function(data){
$(data).find("a:contains(.jpg)").each(function(){
// will loop through
var filename = $(this).attr("href");
$('<li></li>').html('<img src="thumbnail.php?src='+imageUrl+'/'+filename+'&q='+imageQuality+'&h='+finalImageHeight+'&w='+finalImageWidth+'"/>').appendTo('#'+$(this));
});
}
});
};
}(jQuery));
「console.log(this)」怎麼辦? alert()方法只能處理字符串。順便說一句,在調試目的中使用alert()並不推薦,因爲模態行爲 –
你認爲'this'是什麼? – charlietfl