$(this) 不适用于 Flickr Gallery
$(this) not working with Flickr Gallery
请在此处查看我的代码笔:http://codepen.io/dsm/pen/ewEJb
此应用程序将显示两个Flickr画廊。我正在尝试将函数分离以完全独立于函数调用。但是,我目前可以让它工作的唯一方法是对行进行硬编码以包含div id '#flickr'。我尝试改用$(this),但似乎不合作。
有什么建议吗?
$('#flickr').append('<h2>'+photosetTitle+'</h2>');
$('#flickr').append(theHtml);
以上是有问题的代码行,可以在下面的代码中看到。
注意:此代码假定 jquery 已经加载。
(function($){
$.fn.flickrSetGallery = function(callerSettings) {
var settings = $.extend(callerSettings);
var url = 'http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=';
url += settings.apiKey;
url += '&photoset_id=';
url += settings.photosetID;
url += '&format=json&jsoncallback=?';
$.getJSON(url, displayImages);
function displayImages(data) {
var photosetTitle = data.photoset.title;
var photosetTag = photosetTitle.toLowerCase().trim().split(/'s+/).join
var theHtml = "";
$.each(data.photoset.photo, function(i,photo){
var source = 'http://farm'+photo.farm+'.static.flickr.com/'+photo.server+'/'+photo.id+'_'+photo.secret+'_s.jpg';
theHtml+= '<li><a href="http://farm'+photo.farm+'.static.flickr.com/'+photo.server+'/'+photo.id+'_'+photo.secret+'_b.jpg" data-lightbox="'+photosetTag+'">';
theHtml+= '<img title="'+photo.title+'" src="'+source+'" alt="'+photo.title+'" />';
theHtml+= '</a></li>';
});
$('#flickr').append('<h2>'+photosetTitle+'</h2>');
$('#flickr').append(theHtml);
};
};
})(jQuery);
$(document).ready(function() {
$('#flickr').flickrSetGallery({
apiKey: '2eabc9d4b3a1b7443b2900a729b8b4a8',
photosetID: '72157616127960344'
});
$('#flickr2').flickrSetGallery({
apiKey: '403e03feaf4819a91a02f158a0504075',
photosetID: '72157637557971086'
});
});
这是 HTML:
<div id="flickr" class="flickr-things">
</div>
<div id="flickr2" class="flickr-things">
</div>
尝试这样,在回调中使用this
不会将原始元素定位为 jqXhr 对象,因此您可以将其缓存到变量中并使用它:
在 tha ajax 调用之前:
var flickrContainer = this; //this is already a jquery object here
$.getJSON(url, displayImages);
......
并在您的回调中将其用作:
flickrContainer.append('<h2>'+photosetTitle+'</h2>');
flickrContainer.append(theHtml);
演示
还有其他方法,例如使用 $.proxy
function.bind
等将 ajax 回调的上下文绑定到元素的上下文。
$.getJSON(url, displayImages.bind(this)); // or $.proxy(displayImages,this)
现在this
函数内部表示 flickerContainer,所以你可以只做:
this.append('<h2>'+photosetTitle+'</h2>');
this.append(theHtml);
这是因为$(this)
不是包含闪烁元素的jQuery对象,直到您return the object from the plugin
return this.each(function(){
var $this = $(this);
//now $this contains the jQuery object of the element/s bound to the plugin
});
相关文章:
- JQueryhide()不适用于Mozzilla,但适用于Chrome
- PHP中的setcookie仅适用于localhost
- html5 drawImage适用于firefox,而不是chrome
- 提供“;onClick"适用于iPad(触摸屏)和桌面用户的默认功能
- jQuery表单验证适用于Mozilla和Internet Explorer,但不适用于Chrome或Safari
- ResolveUrl是否适用于././也
- Firebase updateChildValues适用于IOS,但不适用于Web和Android
- JavaScript警报适用于int,但不适用于string
- SQLite插件适用于Mac和Windows,但不适用于手机上的Safari
- Regex Replace仅适用于Last Match
- window.onload适用于aspx页面,但不适用于普通html
- Jquery Ajax POST不工作.适用于GET
- 画布上的自定义字体仅适用于safari
- Javascript仅适用于alert()和Debug模式
- JS适用于Firefox和Safari,但不适用于Chrome.此处'是我的网站
- Regex不适用于Firefox,但适用于Chrome
- 通过单击主菜单外部关闭子菜单'不适用于IE,但适用于Firefox&铬
- AngularJS$http.post没有'不适用于Chrome,仅适用于IE
- Javascript仅适用于jQuery mobile中的页面刷新
- 关闭当前选项卡并打开一个新的-仅适用于Chrome