jquery collect value of list items and place in array

前端 未结 2 1793
情书的邮戳
情书的邮戳 2020-12-19 05:20

If I have the following HTML:

  • List 1
  • list 2
  • list 3
相关标签:
2条回答
  • 2020-12-19 06:03
    var x = [];
    $("ul li").each(function() {
      x.push($(this).text());
    });
    

    or simply:

    var x = $.map($("ul li"), function( i ) { return $(i).text(); });
    
    0 讨论(0)
  • 2020-12-19 06:16
    var arr = $("li").map(function() { return $(this).text() }).get();
    
    • The map()(docs) method creates a jQuery object populated with whatever is returned from the function (in this case, the text content of each <li> element).

    • The get()(docs) method (when passed no argument) converts that jQuery object into an actual Array.

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