Get array of text values using selector instead of iterating?

后端 未结 7 2048
长情又很酷
长情又很酷 2020-12-14 00:18

I have some HTML like:

相关标签:
7条回答
  • 2020-12-14 00:55

    Or a little bit shorter than the accepted answer:

    $.map($("#foo span a"), $.text)
    
    0 讨论(0)
  • 2020-12-14 00:57

    You can do it using .map() like this:

    var myArray = $("#foo span a").map(function() {
                     return $(this).text();
                  }).get();
    

    You can test it out here.

    0 讨论(0)
  • 2020-12-14 00:58

    Additionally, the function to get the array of values with trimmed spaces:

    var array = $('li').map(function(){
                   return $.trim($(this).text());
                }).get();
    
    0 讨论(0)
  • 2020-12-14 01:05

    use the jquery selector:

    $("ul#foo span a")
    
    0 讨论(0)
  • 2020-12-14 01:12

    Try this:

    $('#foo span a').map(function() { return $(this).text(); }).get();
    
    0 讨论(0)
  • 2020-12-14 01:13

    you can do it like this

    var texts = new Array();
    
    $('#foo > span > a').each(function() 
    { 
      texts.push( $( this ).text() ); 
    });
    
    0 讨论(0)
提交回复
热议问题