4
$('.myElem').live('click', function() { 
    $(this).hide(500, function() { 
     $(this).siblings('.myOtherElem').show(); 
    }); 
}); 

$(this)이 콜백에서 더 이상 올바른 범위에 없기 때문에 위의 코드가 작동하지 않습니다. 원본 소스 요소를 콜백에 어떻게 전달합니까?jQuery 콜백에서 소스 요소 가져 오기

답변

7

실제로 코드가 작동해야합니다. 만약 외부에있어서 범위의 기준 저장할 수있는 내부 자바 방법 내 this 액세스하려면

:

$('.myElem').on('click', function() { 
    // This refers to the clicked element 
    $(this).hide(500, function() { 
     // This refers to the clicked element as well 
     $(this).siblings('.myOtherElem').show(); 
    });  
}); 
대부분의 jQuery 방법 this에서 그러나

$('.myElem').on('click', function() { 

    var myElem = this;  
    $(this).hide(500, function() { 
     $(myElem).siblings('.myOtherElem').show(); 
    }); 

}); 

사용 선택기 또는 요소를 참조한다

2
$('.myElem').live('click', function() { 
    var $this = $(this); 
    $this.hide(500, function() { 
     $this.siblings('.myOtherElem').show(); 
    }); 
}); 
0
$('.myElem').live('click', function() { 
    $(this).hide(500); 
    $(this).siblings('.myOtherElem').show(); 
}); 
+0

같은 일을 수행하지 않습니다. '.show()'호출을 콜백에두면'.hide()'애니메이션이 완료 될 때까지 일어나지 않습니다. 답안의 코드는 둘 다 거의 동시에 일어날 것입니다. –

+0

'delay'를 사용하면됩니다 :'.delay (500) .show (1)'그러나'show' 콜백을 사용하는 것이 더 나은 해결책입니다. – jantimon