javascript - jquery `this` 中的特定元素

标签 javascript jquery this

因此,在 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/

相关文章:

javascript - 如何在本地的html代码中使用javascript脚本

javascript - 页面未链接到 Jquery

javascript 原型(prototype)对象 this 值

javascript - Bourbon Refills Navigation JavaScript 错误 : Uncaught TypeError: undefined is not a function

javascript - 是否可以用鼠标在 d3.js 条形图上选择多个条形

javascript - vue js 可以在自定义组件模板中使用 v-for 吗?

javascript - 使用 JavaScript/jQuery 创建交互式 map

android - EnableListAdapter 中的上下文

c++ - 'this'指针是否参与虚函数多态行为

javascript - 如何使用jquery的ajax方法从php文件返回整数值?