因此,在 jquery 中,如果我想在单击时定位 .test
类中的 div,我使用以下代码:
$('.test div').click(function(){
//do something
});
但是如果“做某事”部分需要 this
参数,我似乎无法获得相同的效果。假设我想让任何带有 test
类的 div 中的所有粗体文本淡出。我必须做这样的事情:
$('.test div').click(function(){
$(this ' b').animate({
opacity:toggle
},500);
});
但这是无效的代码。那么我该如何定位 this
Jquery 选择器中的特定元素呢?
最佳答案
您需要将上下文作为第二个参数传递给 jQuery
$('b', this).animate({
opacity:toggle
},500)
另一个解决方案是使用 .find()
$(this).find('b').animate({
opacity:toggle
},500)
关于javascript - jquery `this` 中的特定元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17101566/