Javascript 如何从jquery插件中引用主题$(this)?

Javascript 如何从jquery插件中引用主题$(this)?,javascript,jquery,Javascript,Jquery,我正在使用,我只是得到未定义的作为我的获取值 $('.banner').colorbox({ opacity: 0.4, href: 'dialogs/ban_add_edit.php?banner_to_edit='+$(this).attr('id')+'&typeofbanner='+$(this).attr('rel') }) 您可以这样做: $('.banner').each(function() { $(this).col

我正在使用,我只是得到未定义的作为我的获取值

$('.banner').colorbox({
    opacity: 0.4,
    href: 'dialogs/ban_add_edit.php?banner_to_edit='+$(this).attr('id')+'&typeofbanner='+$(this).attr('rel')            
    })
您可以这样做:

$('.banner').each(function() {
  $(this).colorbox({
    opacity: 0.4,
    href: 'dialogs/ban_add_edit.php?banner_to_edit='+this.id+'&typeofbanner='+$(this).attr('rel')            
  });
});
在您当前的代码中,此指的是您正在运行它的任何东西,可能是一个
document.ready
函数(因此
=
文档
)。在这个版本中,您在
.banner
元素中循环,而
这个
指的是您循环时所在的元素


还有一个变化是
this.id
,我经常这样做,但不需要
$(this.attr('id')
,除非您需要稍后处理它被链接…
this.id
原始DOM样式更短更快:)

非常有用,也感谢使用this对象的技巧H@J-我想我不喜欢它。