开发者

jquery collect value of list items and place in array

开发者 https://www.devze.com 2023-02-07 15:04 出处:网络
If I have the following HTML: <ul> <li>List 1</li> <li>list 2</li> <li>list 3</li>

If I have the following HTML:

<ul>
  <li>List 1</li>
  <li>list 2</li>
  <li>list 3</li>
</ul>

Can I get the text content from the the <li>'s and place them in a开发者_JS百科rray using javascript?


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.


var x = [];
$("ul li").each(function() {
  x.push($(this).text());
});

or simply:

var x = $.map($("ul li"), function( i ) { return $(i).text(); });
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号