jquery收集列表项的值并放在数组中 [英] jquery collect value of list items and place in array

查看:67
本文介绍了jquery收集列表项的值并放在数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有以下HTML:

If I have the following HTML:

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

我可以从< li> 并使用javascript将它们放在数组中?

Can I get the text content from the the <li>'s and place them in array using javascript?

推荐答案

var arr = $("li").map(function() { return $(this).text() }).get();




  • map() (docs) 方法创建一个jQuery对象,其中填充了从函数返回的任何内容(在这种情况下,每个< li> 元素的文本内容)。

    • 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).

      get() (docs) 方法(当没有参数传递时)将该jQuery对象转换为实际的数组。

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

      这篇关于jquery收集列表项的值并放在数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆