【问题标题】:this.id inside of fancybox花式框内的 this.id
【发布时间】:2011-01-27 05:55:50
【问题描述】:

我试图在标题中弹出一个带有 facebook LIKE 按钮的花式框,该按钮根据单击的项目链接到唯一页面。所有图像都使用唯一编号进行标识,我只是尝试使用 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;
            }
        });
    });
});

【问题讨论】:

    标签: javascript jquery facebook fancybox


    【解决方案1】:

    您试图在与.each 函数不同的函数内访问this,因此this 指的是其他内容。您可以将this 复制到变量中,以便在内部函数中安全访问:

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

    【讨论】:

      猜你喜欢
      • 2011-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多