this.id inside of fancybox

跟風遠走 提交于 2019-12-11 20:45:58

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/4813197/this-id-inside-of-fancybox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!