JQuery Multiple selectors, $(this) reference?

后端 未结 3 727
隐瞒了意图╮
隐瞒了意图╮ 2021-01-18 07:56

given the following

$(\"#identifier div:first, #idetifier2\").fadeOut(300,function() {
  // I need to reference just the \'#identifier div:first\' element
           


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-18 08:00

    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.

提交回复
热议问题