加载中...

map()


概述    .map( callback(index, domElement) )

返回值:jQuery

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

  • V : 1.2.map( callback(index, domElement) )

    • callback(index, domElement)
      类型: Function()
      一个函数对象,会在处理集合中的每个元素时被调用。

如果你想处理一个简单的数组或对象中,使用jQuery.map()代替。

由于返回值是一个jQuery包裹的数组,所以通常会使用get()方法将其转换成普通的数组。

.map()方法特别适用于获取或设置元素集合中的值。例如,如下的表单中包含一组 checkbox 框:

  1. <form method="post" action="">
  2. <fieldset>
  3. <div>
  4. <label for="two">2</label>
  5. <input type="checkbox" value="2" id="two" name="number[]">
  6. </div>
  7. <div>
  8. <label for="four">4</label>
  9. <input type="checkbox" value="4" id="four" name="number[]">
  10. </div>
  11. <div>
  12. <label for="six">6</label>
  13. <input type="checkbox" value="6" id="six" name="number[]">
  14. </div>
  15. <div>
  16. <label for="eight">8</label>
  17. <input type="checkbox" value="8" id="eight" name="number[]">
  18. </div>
  19. </fieldset>
  20. </form>

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

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

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

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

示例

实例

获取表单中所有表单元素的值。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. p { color:red; }
  6. </style>
  7. <script src="http://code.jquery.com/jquery-latest.js"></script>
  8. </head>
  9. <body>
  10. <p><b>Values: </b></p>
  11. <form>
  12. <input type="text" name="name" value="John"/>
  13. <input type="text" name="password" value="password"/>
  14. <input type="text" name="url" value="http://ejohn.org/"/>
  15. </form>
  16. <script>
  17. $("p").append( $("input").map(function(){
  18. return $(this).val();
  19. }).get().join(", ") );
  20. </script>
  21. </body>
  22. </html>

运行一下

实例

A contrived example to show some functionality.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. body { font-size:16px; }
  6. ul { float:left; margin:0 30px; color:blue; }
  7. #results { color:red; }
  8. </style>
  9. <script src="http://code.jquery.com/jquery-latest.js"></script>
  10. </head>
  11. <body>
  12. <ul>
  13. <li>First</li>
  14. <li>Second</li>
  15. <li>Third</li>
  16. <li>Fourth</li>
  17. <li>Fifth</li>
  18. </ul>
  19. <ul id="results">
  20. </ul>
  21. <script>
  22. var mappedItems = $("li").map(function (index) {
  23. var replacement = $("<li>").text($(this).text()).get(0);
  24. if (index == 0) {
  25. /* make the first item all caps */
  26. $(replacement).text($(replacement).text().toUpperCase());
  27. } else if (index == 1 || index == 3) {
  28. /* delete the second and fourth items */
  29. replacement = null;
  30. } else if (index == 2) {
  31. /* make two of the third item and add some text */
  32. replacement = [replacement,$("<li>").get(0)];
  33. $(replacement[0]).append("<b> - A</b>");
  34. $(replacement[1]).append("Extra <b> - B</b>");
  35. }
  36. /* replacement will be a dom element, null,
  37. or an array of dom elements */
  38. return replacement;
  39. });
  40. $("#results").append(mappedItems);
  41. </script>
  42. </body>
  43. </html>

运行一下

实例

Equalize the heights of the divs.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. div { width: 40px; float:left; }
  6. input { clear:left}
  7. </style>
  8. <script src="http://code.jquery.com/jquery-latest.js"></script>
  9. </head>
  10. <body>
  11. <input type="button" value="equalize div heights">
  12. <div style="background:red; height: 40px; "></div>
  13. <div style="background:green; height: 70px;"></div>
  14. <div style="background:blue; height: 50px; "></div>
  15. <script>
  16. $.fn.equalizeHeights = function() {
  17. var maxHeight = this.map(function(i,e) {
  18. return $(e).height();
  19. }).get();
  20. return this.height( Math.max.apply(this, maxHeight) );
  21. };
  22. $('input').click(function(){
  23. $('div').equalizeHeights();
  24. });
  25. </script>
  26. </body>
  27. </html>

运行一下


还没有评论.