开发者

this.id inside of fancybox

I'm attempting to have a fancybox pop up with a facebook LIKE button in the title that links to the unique page based on which item is clicked. All of the images are id'ed with a unique number and I'm simply trying to get that number with this.id but it is not returning anything.

$(document).ready(function() {
    $("a.fancybox").each(function() {
        $(this).fancybox({
            'overlayShow'   : true,
            'autoDimensions': true,
            'transitionIn'  : 'elastic',
        开发者_JS百科    'transitionOut' : 'elastic',
            'overlayColor'  : '#000',
            'overlayOpacity': 0.7,
            'titlePosition' : 'inside',
            'titleFormat'   : function() {
                var astring = '<span id="fb-title"><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.myurl.com%2Fscript.php%3Fi%3D' + this.id + '&amp;layout=standard&amp;show_faces=false&amp;width=350&amp;action=like&amp;font=lucida+grande&amp;colorscheme=light&amp;height=35" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:350px; height:35px;" allowTransparency="true"></iframe></span>';
                return astring;
            }
        });
    });
});


You're trying to access this inside a different function to the .each function, so this refers to something else. You can copy this into a variable though, for safe access inside the inner function:

$("a.fancybox").each(function() {
    var element = this;
    $(this).fancybox({
        'titleFormat'   : function() {
            var astring = '<span>' + element.id + '</span>';
            return astring;
        }
    });
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜