简体   繁体   中英

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',
            '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;
        }
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM