Mootools 1.2 Tutorial (2) DOM Selector_Mootools
If you are not ready yet, please read the previous article "Mootools 1.2 Tutorial (1) - Introduction to MooTools" first. We talked about how to reference MooTools 1.2 and how to call your scripts in domready.
Today we start the second lecture of this series of tutorials. In this lesson, we'll learn several ways to select HTML elements. In many ways, this is the most used and basic of MooTools. After all, to create an interactive user experience based on HTML elements, you must first have them in your hands.
Basic methods
$();
$ function is the basic selector in MooTools. You can use it to select DOM elements based on an ID.
Reference code:
// Select the ID as " body_wrap" element
$('body_wrap');
Reference code:
.getElement();
. getElement(); extends the $ method, allowing you to simplify your selection operations. For example, you can use the $ method to select the element with the ID "body_wrap" and then select the first child node. .getElement(); Selects only one element. If there are multiple elements that meet the requirements, the first element is returned. If you give the .getElement(); method a CSS class name as a parameter, you will get the first element with that CSS class name, rather than an array of all elements of the function. To select multiple elements, you can use the .getElements(); method below.
Reference code:
// Select the ID as " The first link under the element with the ID "body_wrap"
$('body_wrap').getElement('a');
// Select the element with the ID "special_anchor" below the element with the ID "body_wrap"
$('body_wrap').getElement('#special_anchor');
// Select the first element with CSS class name "special_anchor_class" under the element with ID "body_wrap"
$('body_wrap ').getElement('.special_anchor_class');
Reference code:
$$ ();
The $$ function allows you to quickly select multiple elements and form an array (a list that you can manipulate, retrieve, and reorder in any way). You can select multiple elements by tag name (such as div, a, img, etc.), or ID, or various combinations of them. As one reader pointed out, you can do a lot more with $$ than we've covered here.
Reference code:
// Select this page All divs
$$('div');
// Select the element with ID "id_name and all divs
$$('#id_name', 'div');
Reference code:
a span
. getElements();
.getElements(); is very similar to .getElement();, but it returns all elements that meet the requirements and form an array. You can use .getElements( like the .getElement(); method. );.
Reference code:
// Select all links under the element with ID "body_wrap"
$('body_wrap').getElements('a');
// Select the element with ID "body_wrap" All the child elements with the CSS class name "special_anchor_class" below
$('body_wrap').getElements('.special_anchor_class');
Reference code:
Include and exclude with operators Results
Operators
MooTools 1.2 supports several operators that allow you to further streamline your selection operations. You can use these operators in .getElements(); to include or exclude specific results. MooTools supports 4 operators, each of which can be used to select an input element by name.
= : equal to
Reference code:
//Select the input element with name "phone_number"
$('body_wrap').getElements('input[name=phone_number]');
^=: Start with...
Reference code:
//Select the input element whose name starts with "phone"
$('body_wrap').getElements('input[name^=phone]' ; $=number]');
!= : Not equal to
Reference code:
// Select the input element whose name is not equal to "address"
$('body_wrap').getElements('input [name!=address]');
Reference code:
(Fdream note: Input is just an example here, you can also use this method to select other elements, such as div, a, etc.)
To use the operator, you must first specify the type of the element (such as input here), and then Specify the attribute you want to filter (such as name here), plus your operator, and finally select your filter string.
Selector based on element order
even (even number selection)
With this simple selector, you can select elements with even numbers. Note: This selector starts counting from 0, so the first element is in even order.
Reference code:
// Select the div with an even number
$$('div:even');
Reference code:
odd (odd selection)
Same as even, except that it selects elements with odd numbers.
Reference code:
// Select all divs with odd numbers
$$('div:odd');
Reference code:
.getParent();
Through the .getParent(); method, you can get the parent element (parent) of an element.
Reference code:
// Select the parent element of the element with ID "child_id"
$('child_id').getParent();
Reference code:
Code example
Any MooTools UI development starts with selectors. Here are some very simple examples demonstrating how to use selectors to manipulate DOM elements.
Reference code:
// Set the background color of all spans to #eee
$$('span').setStyle('background-color', '#eee');
// Set the background color of all spans with odd numbers to #eee
$$('span:odd').setStyle('background-color', '#eee');
// Set the ID to body_wrap The background color of all spans with the CSS class name .middle_spans under the element is #eee
$('body_wrap').getElements('.middle_spans').setStyle('background-color', '#eee');
Reference code:
Copy code
Even
Odd
< ;span class="middle_spans">Even
Odd
Download the zip package and Try it
This zip package contains a simple html file, the MooTools 1.2 core library, an external js file and the example you see above.
Learn more…
This does not mean that this is the complete list of features of the selector in MooTools 1.2, it is just to help you get started and tell you what features MooTools provides you. To learn more about selectors, please refer to the following documentation:
- There is a lot of documentation about the Element selector
- By the way, you can also take a lookSelectors
Article about $$ selector on MooTools Blog
This is a very good blog post on mootools.net about the $$ selector and its many variations . You’ll never believe what you can do with this selector, and this article is well worth a read.
Slickspeed Selector
Here is an experiment done by others on MooTools to measure how fast different libraries are when selecting elements. This is cool in its own right, but these selector examples are very valuable. All selector features here can be implemented through the $$ method.
W3C Selector
MooTools also lets you harness the power of pseudo-selectors (as demonstrated by Slickspeed above). Here is a W3C article about selectors that is definitely worth reading (if only the list of selectors is useful to you). I'm not sure if MooTools' $$ selector supports every single selector on this page, but at least most of them. Feel free to tell me more about this.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



1. Native js gets the DOM node: document.querySelector (selector) document.getElementById (id selector) document.getElementsByClassName (class selector).... 2. Get the instance object of the current component in vue2: because each vue Each component instance contains a $refs object, which stores references to the corresponding DOM elements or components. So by default, the component's $refs point to an empty object. You can first add ref="name" to the component, and then pass this.$refs.

Use the :nth-child(n+3) pseudo-class selector to select the style of child elements whose position is greater than or equal to 3. The specific code example is as follows: HTML code: <divid="container"><divclass="item"> ;First child element</div><divclass="item"&

In web development, DOM (DocumentObjectModel) is a very important concept. It allows developers to easily modify and operate the HTML or XML document of a web page, such as adding, deleting, modifying elements, etc. The built-in DOM operation library in PHP also provides developers with rich functions. This article will introduce the DOM operation guide in PHP, hoping to help everyone. The basic concept of DOM DOM is a cross-platform, language-independent API that can

Vue3ref binding DOM or component failure reason analysis scenario description In Vue3, it is often used to use ref to bind components or DOM elements. Many times, ref is clearly used to bind related components, but ref binding often fails. Examples of ref binding failure situations The vast majority of cases where ref binding fails is that when the ref is bound to the component, the component has not yet been rendered, so the binding fails. Or the component is not rendered at the beginning and the ref is not bound. When the component starts to render, the ref also starts to be bound, but the binding between ref and the component is not completed. At this time, problems will occur when using component-related methods. The component bound to ref uses v-if, or its parent component uses v-if to cause the page to

The JavaScript selector fails because the code is not standardized. The solution is: 1. Remove the imported JS code and the ID selector method will be effective; 2. Just introduce the specified JS code before introducing "jquery.js".

There are 5 DOM objects including "document", "element", "Node", "Event" and "Window"; 2. "window", "navigator", "location" and "history" and "screen" and other 5 BOM objects.

From beginner to proficient: Master the skills of using is and where selectors Introduction: In the process of data processing and analysis, the selector is a very important tool. Through selectors, we can extract the required data from the data set according to specific conditions. This article will introduce the usage skills of is and where selectors to help readers quickly master the powerful functions of these two selectors. 1. Use of the is selector The is selector is a basic selector that allows us to select the data set based on given conditions.

In-depth analysis of is and where selectors: improving the level of CSS programming Introduction: In the process of CSS programming, selectors are an essential element. They allow us to select and style elements in an HTML document based on specific criteria. In this article, we will take a deep dive into two commonly used selectors namely: is selector and where selector. By understanding their working principles and usage scenarios, we can greatly improve the level of CSS programming. 1. is selector is selector is a very powerful choice
