Home >Web Front-end >JS Tutorial >Mootools 1.2 tutorial (3) Introduction to using arrays_Mootools

Mootools 1.2 tutorial (3) Introduction to using arrays_Mootools

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-16 18:46:401075browse

Today, we'll look at how to use arrays to manage DOM elements.
Basic methods
.each();
When dealing with arrays, the .each(); method is your best friend. It provides an easy way to iterate over each element of an array and perform any logical processing on the elements if necessary. For example, we can assume that you need to call the alert method for each div object in the page:
Reference code:

Copy code The code is as follows :

$$('div').each(function() {
alert('a div');
});

If you use the HTML code below, the JavaScript code above will pop up two alert dialog boxes, one for each div.
Reference code:
Copy code The code is as follows:

One

Two

The
.each(); method does not require you to use the $$ method. Another way to create an array (like we covered yesterday) is to use the .getElements(); method.
Reference code:
Copy code The code is as follows:

$('body_wrap').getElements( 'div').each(function() {
alert('a div');
});

Reference code:
Copy code The code is as follows:


One

Two



There is another way to accomplish the same task, which is to assign the array to a variable and then That variable uses the .each(); method:
Reference code:
Copy code The code is as follows:

// First you need to declare a variable through the statement "var VARIABLE_NAME"
// Then use the equal operator "=" to assign a value to this variable
// In this example, it is a variable containing a div element Array
var myArray = $('body_wrap').getElements('div');
// Now you can use this variable as an array selector
myArray.each(function() {
alert('a div');
});

Finally, if you might want to separate your function from the selector. We'll cover this in more depth in tomorrow's tutorial on using functions. However, now we can create a very simple example:
Reference code:
Copy code The code is as follows:

var myArray = $('body_wrap').getElements('div');
// To create a function, you can declare a variable as before and give it a name
// Waiting Use "function()" after the number to declare this variable as a function
// Finally, write your function code between { and }
var myFunction = function() {
alert(' a div');
};
// Now you can call the function just now in the .each();. method
myArray.each(myFunction);

Note: When you call a function in the .each();. method as just now, you do not need to put quotes around the function name.
Copy an array
$A
MooTools provides a simple way to copy an array through the $A function. Let’s create an array using variables as we just did:
Reference code:
Copy code The code is as follows:

// Create your array variable
var myArray = $('body_wrap').getElements('div');

Copy an array (create a copy of the array):
Reference code:
Copy code The code is as follows:

// Create a new variable name and name it for "myCopy", and then assign a copy of "myArray" to it
var myCopy = $A(myArray );

Get the specified element from the array
.getLast();
.getLast(); method returns the last element in the array. First we create an array:
Reference code:
Copy code The code is as follows:

var myArray = $('body_wrap').getElements('div');

Now we can get the last element from this array:
Reference code:
Copy code The code is as follows:

var lastElement = myArray.getLast();

The current value of variable lastElement is The last element in the array myArray.
.getRandom();
Same as .getLast();, but it randomly gets an element from the array:
Reference code:
Copy code The code is as follows:

var randomElement = myArray.getRandom();

The current value of variable randomElement is randomly selected from the array myArray of an element.
Add an element to the array
.include();
With this method, you can add another element to the array. Just pass the element selector to the .include(); method and it will be included in your array. We use the following HTML code:
Reference code:
Copy code The code is as follows:

< div id="body_wrap">
one

two

add to array


We can create an array by calling all divs under "body_wrap" as before:
Reference code:
Copy code The code is as follows:

var myArray = $('body_wrap').getElements('div');

To add another element to this array, first you need to assign this element to a variable, and then use the include method:
Reference code:
Copy the code The code is as follows:

// First assign your element to a variable
var newToArray = $('add_to_array');
// Then add it to the array
myArray.include(newToArray);

Now, the array contains both div and span elements.
.combine();
Same as .include();, but it allows you to add an array to an existing array without worrying about duplicate content. Suppose we now obtain two arrays from the following HTML:
Reference code:
Copy code The code is as follows:


one

two

add to array
add to array, also
add to array, too< /span>


We can create two arrays like this:
Reference code:
Copy code The code is as follows:

// Create your array just like we did before
var myArray= $('body_wrap').getElements('div');
// Then create an array of elements with all CSS classes named .class_name
var newArrayToArray = $$('.class_name');

Now we can use .combine() ;Method to merge two arrays, this method will handle duplicate elements by itself, so we don’t need to deal with it:
Reference code:
Copy code The code is as follows:

// Merge the array newArrayToArray into the array myArray
myArray.combine(newArrayToArray);

Now myArray contains all the elements in newArraytoArray.
Code Example
Arrays allow you to iterate through a list containing all items and execute the same code for each element. In this example, note the use of the variable "item" as a substitute for the current element.
Reference code:
Copy code The code is as follows:

// Create an array, this array contains All elements with CSS class name .class_name in "body_wrap"
var myArray = $('body_wrap').getElements('.class_name');
// First create an element to be added to the array
var addSpan = $('addtoarray');
// Then create an array to be merged
var addMany = $$('.addMany');
// Now we add the element addSpan into the array
myArray.include(addSpan);
// Then merge the array addMany into myArray
myArray.combine(addMany);
// Create an array that requires each element in the array Functions that must be executed
var myArrayFunction = function(item) {
// item now points to the current element in the array
item.setStyle('background-color', '#eee');
}
// Now call the myArrayFunction function for each item in the array
myArray.each(myArrayFunction);

Reference code:
Copy code The code is as follows:


one< /div>
two

three

add to array

one of many

two of many


Extended Learning

This tutorial is not intended to cover everything you can do with arrays, but hopefully it will give you a reference and tell you what features MooTools provides. To learn more about arrays, read these carefully:

Download a zip package with everything you need to get started

Includes a simple html file, MooTools 1.2 core library, an external JavaScript file, a css file and all the examples above.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn