Jquery map()

<!DOCTYPE html>
<html>
<head>
  <style>p { color:red; }</style>
  <script type="text/javascript" src="/jquery/jquery.js"></script>
</head>

<body>
  <p><b>Values: </b></p>
  <form>
    <input type="text" name="name" value="John"/>
    <input type="text" name="password" value="password"/>
    <input type="text" name="url" value="http://w3school.com.cn/"/>
  </form>

<script>
    $("p").append( $("input").map(function(){
      return $(this).val();
    }).get().join(", ") );
</script>

</body>
</html>

定义和用法

map() 把每个元素通过函数传递到当前匹配集合中,生成包含返回值的新的 jQuery 对象。

由于返回值是 jQuery 封装的数组,使用 get() 来处理返回的对象以得到基础的数组。

posted @ 2016-10-14 16:07  Chris_在IT道路上前行  阅读(145)  评论(0编辑  收藏  举报