jquery select class inside parent div

后端 未结 3 1371
天命终不由人
天命终不由人 2020-12-15 05:13

I\'m trying to change the alt of the image, I\'m clicking by selecting the image\'s class (add_answer)

Note:

相关标签:
3条回答
  • 2020-12-15 05:31

    you can use the parent div as the scope:

     $('.add_answer',$(this).parent('div:first')).attr('alt',count);
    
    0 讨论(0)
  • 2020-12-15 05:37

    This should work:

    $(this).parent("div").find(".add_answer").attr("alt", count);
    
    0 讨论(0)
  • 2020-12-15 05:45

    You code is almost correct. Required change is to use .find instead of .$ after .parents method. Use of .parent instead of .parents should be avoided, this way your code will be more unobtrusive (precisely - this way img can be non-direct child of the div).

    $(this).parents('div:eq(0)').find('.add_answer')
    

    You can manipulate :eq(0) to select eg third parent div using :eq(2).

    0 讨论(0)
提交回复
热议问题