使用jquery在`this`中的特定元素

Rya*_*axe 0 javascript jquery this

所以在jquery中,如果我想在.test单击中定位类中的div,我使用以下代码:

$('.test div').click(function(){
    //do something
});
Run Code Online (Sandbox Code Playgroud)

但如果"做某事"部分需要一个this参数,我似乎无法获得相同的效果.所以我想说我希望在任何div中制作所有粗体文本,并且test淡出.我必须做这样的事情:

$('.test div').click(function(){
    $(this ' b').animate({
        opacity:toggle
    },500);
});
Run Code Online (Sandbox Code Playgroud)

但那是无效的代码.那么我如何定位thisJquery选择器中的特定元素?

Aru*_*hny 6

您需要将上下文作为第二个参数传递给jQuery

 $('b', this).animate({
     opacity:toggle
 },500)
Run Code Online (Sandbox Code Playgroud)

另一个解决方案是使用.find()

 $(this).find('b').animate({
     opacity:toggle
 },500)
Run Code Online (Sandbox Code Playgroud)