最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

.map()

.map( callback(index, domElement) ) 返回: jQuery

描述: 通過一個函數(shù)匹配當前集合中的每個元素,產(chǎn)生一個包含的返回值的jQuery新對象。

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

    callback(index, domElement)一個函數(shù)對象,將調用當前集合中的每個元素。

作為返回值是一個jQuery包裝的數(shù)組,這是非常常見get()返回的對象與基本數(shù)組。

.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".

在回調函數(shù)中,this指的是每次迭代當前DOM元素。該函數(shù)可以返回一個單獨的數(shù)據(jù)項目或數(shù)據(jù)項的數(shù)組并在結果集合中插入。如果數(shù)組返回,數(shù)組中的元素插入到集合。如果函數(shù)返回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 中文版腳本之家整理、修訂 (2011年6月)
南康市| 鹿泉市| 辛集市| 安宁市| 龙门县| 辰溪县| 汤原县| 平利县| 繁昌县| 湄潭县| 禄劝| 醴陵市| 育儿| 营口市| 驻马店市| 洛阳市| 遂宁市| 出国| 博兴县| 景德镇市| 靖边县| 霍城县| 大丰市| 来安县| 磴口县| 娄底市| 进贤县| 宁夏| 乌拉特中旗| 进贤县| 穆棱市| 华坪县| 淅川县| 连山| 社旗县| 科技| 荔浦县| 时尚| 大安市| 大竹县| 高邑县|