Get an array of list element contents in jQuery

后端 未结 6 1351
日久生厌
日久生厌 2020-11-27 11:33

I have a structure like this:

  • text1
  • text2
  • text3

6条回答
  •  长情又很酷
    2020-11-27 12:03

    kimstik was close, but not quite.

    Here's how to do it in a convenient one-liner:

    $.map( $('li'), function (element) { return $(element).text() });
    

    Here's the full documentation for jQuery's map function, it's quite handy: http://api.jquery.com/jQuery.map/

    Just to answer fully, here's the complete functionality you were looking for:

    $.map( $('li'), function (element) { return $(element).text() }).join(', ');
    

提交回复
热议问题