jQuery多个选择器,$(this)参考? [英] JQuery Multiple selectors, $(this) reference?

查看:76
本文介绍了jQuery多个选择器,$(this)参考?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑到以下情况

$("#identifier div:first, #idetifier2").fadeOut(300,function() {
  // I need to reference just the '#identifier div:first' element
  // however $(this) will grab both selectors
});

除了再次调用$(#identifier div:first")之外,还有更好的方法吗?

Is there a better way to go about this other than just calling $("#identifier div:first") again?

推荐答案

否,它将分别为每个句柄调用该函数.

No, it'll call the function for each handle separately.

选择器中的逗号等于说:

The comma in your selector is equivalent to saying:

$("#identifier div:first").fadeOut(300,function() {
  // $(this) -> '#identifier div:first'
  });

$("#idetifier2").fadeOut(300,function() {
   // $(this) -> '#identifier2'
});

您可以说(未经测试):

You can check by saying (untested):

$("#identifier div:first, #idetifier2").fadeOut(300,function() {
  if($(this).is("#identifier div:first")  {
     // do something
  }
});

但是,如果您想做不同的事情(如您的帖子所显示的那样),则最好将它们分别附加.

However, if you want to do different things (as what seems from your post), its better to attach them separately.

这篇关于jQuery多个选择器,$(this)参考?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆