jQuery selector inside the each() method

后端 未结 5 1520
暗喜
暗喜 2020-12-31 12:32

Lets say that I have a HTML that looks like this:

1
2
相关标签:
5条回答
  • 2020-12-31 12:37

    $(obj).find('span') should do the trick.

    0 讨论(0)
  • 2020-12-31 12:39

    easiest way is this, if you want all the elements

    $('.aaa span');
    

    jquery can nest selectors just like css can. also, if for some reason you need to loop

    $('.aaa').each(function(){
        x = $(this).find('span');
    });
    

    that will set x as the elements as a jquery object.

    0 讨论(0)
  • 2020-12-31 12:54
    $('.aaa').each(function(index, obj){
        var x = $(this).find('span');
        $(x).doSomething();
    })
    

    or more prgramatically:

    $('.aaa').each(function(index, obj){
        $(this).find('span').doSomething();
    })
    
    0 讨论(0)
  • 2020-12-31 12:55
    $('.aaa').each(function() {
      var x = $('span', this);
    });
    
    0 讨论(0)
  • 2020-12-31 13:01

    If that's your actual markup, you can easily use the native property firstChild.

    $('.aaa').each(function(){
        var x = this.firstChild
    });
    

    It is a widely supported property.

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