ドキュメント

Utilities/jQuery.makeArray

From jQuery JavaScript Library

Jump to: navigation, search

« Back to Utilities

jQuery.makeArray( obj )

Turns anything into a true array.
Typically it will be unnecessary to use this function if you are using jQuery which uses this function internally.
Arguments:
objObject
Anything to turn into an actual Array.
Examples:

Turn a collection of HTMLElements into an Array of them.

    var arr = jQuery.makeArray(document.getElementsByTagName("div"));
    arr.reverse(); // use an Array method on list of dom elements
    $(arr).appendTo(document.body);

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  
  <script>
  $(document).ready(function(){
    
    var arr = jQuery.makeArray(document.getElementsByTagName("div"));
    arr.reverse(); // use an Array method on list of dom elements
    $(arr).appendTo(document.body);

  });
  </script>
  <style>
  div { color:red; }
  </style>
</head>
<body>
  <div>First</div>
  <div>Second</div>  
  <div>Third</div>
  <div>Fourth</div>
</body>
</html>

NameType