.map()

.map( callback(index, domElement) ) 官网原E文: jQuery

描述: 通过一个函数匹配当前集合中的每个元素,产生一个包含的返回值的jQuery新对象。

  • version added: 1.2.map( callback(index, domElement) )

    callback(index, domElement)一个函数对象,将调用当前集合中的每个元素。

作为返回值是一个jQuery包装的数组,这是非常常见get()返回的对象与基本数组。

.map()方法尤其有用于元素获取或设置一个集合的值。考虑一个复选框集合的表单:

<form method="post" action="">
  <fieldset>
    <div>
      <label for="two">2</label>
      <input type="checkbox" value="2" id="two" name="number[]">
    </div>
    <div>
      <label for="four">4</label>
      <input type="checkbox" value="4" id="four" name="number[]">
    </div>
    <div>
      <label for="six">6</label>
      <input type="checkbox" value="6" id="six" name="number[]">
    </div>
    <div>
      <label for="eight">8</label>
      <input type="checkbox" value="8" id="eight" name="number[]">
    </div>
  </fieldset>
</form>

我们可以得到一个用逗号分隔的复选框 ID

$(':checkbox').map(function() {
  return this.id;
}).get().join(',');

此调用的结果是字符串, "two,four,six,eight".

在回调函数中,this指的是每次迭代当前DOM元素。该函数可以返回一个单独的数据项目或数据项的数组并在结果集合中插入。如果数组返回,数组中的元素插入到集合。如果函数返回nullundefined ,没有元素将被插入。

Examples:

Example: Build a list of all the values within a form.

<!DOCTYPE html>
<html>
<head>
  <style>
  p { color:red; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.min.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://ejohn.org/"/>

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

</script>

</body>
</html>

Demo:

Example: A contrived example to show some functionality.

<!DOCTYPE html>
<html>
<head>
  <style>
  body { font-size:16px; }
  ul { float:left; margin:0 30px; color:blue; }
  #results { color:red; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
  <ul>
    <li>First</li>
    <li>Second</li>
    <li>Third</li>

    <li>Fourth</li>
    <li>Fifth</li>
  </ul>
  <ul id="results">

  </ul>
<script>
    var mappedItems = $("li").map(function (index) {
      var replacement = $("<li>").text($(this).text()).get(0);
      if (index == 0) {
        // make the first item all caps
        $(replacement).text($(replacement).text().toUpperCase());
      } else if (index == 1 || index == 3) {
        // delete the second and fourth items
        replacement = null;
      } else if (index == 2) {
        // make two of the third item and add some text
        replacement = [replacement,$("<li>").get(0)];
        $(replacement[0]).append("<b> - A</b>");
        $(replacement[1]).append("Extra <b> - B</b>");
      }

      // replacement will be an dom element, null, 
      // or an array of dom elements
      return replacement;
    });
    $("#results").append(mappedItems);

</script>

</body>
</html>

Demo:

Example: Equalize the heights of the divs.

<!DOCTYPE html>
<html>
<head>
  <style>
div { width: 40px; float:left; }
input { clear:left}
  </style>
  <script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
  

<input type="button" value="equalize div heights">

<div style="background:red; height: 40px; "></div>
<div style="background:green; height: 70px;"></div>
<div style="background:blue; height: 50px; "></div>


<script>
$.fn.equalizeHeights = function(){
  return this.height( Math.max.apply(this, $(this).map(function(i,e){ return $(e).height() }).get() ) )
}
$('input').click(function(){
  $('div').equalizeHeights();
});

</script>

</body>
</html>

Demo:

jQuery 1.6 API 中文版Clove整理、修订