简体   繁体   English

fancybox内的this.id

[英]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. 我正在尝试弹出一个带有标题的Facebook LIKE按钮的花式框,该链接链接到基于单击哪个项目的唯一页面。 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. 所有图像都使用唯一的编号标识,而我只是想使用this.id来获取该编号,但是它没有返回任何内容。

$(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. 您试图在与.each函数不同的函数中访问this函数,因此this引用了其他内容。 You can copy this into a variable though, for safe access inside the inner function: 您可以复制this到一个变量但是,对于内部函数内部安全访问:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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