search
HomeWeb Front-endJS TutorialMootools 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:

Copy code The code is as follows:

// Select the ID as " body_wrap" element
$('body_wrap');

Reference code:
Copy code The code is as follows:




.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:
Copy code The code is as follows:

// 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:
Copy code Code As follows:

$$ ();
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:
Copy code The code is as follows:

// Select this page All divs
$$('div');
// Select the element with ID "id_name and all divs
$$('#id_name', 'div');

Reference code:
Copy code The code is as follows:


a div

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:
Copy code The code is as follows:

// 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:
Copy code The code is as follows:

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:
Even

Odd

Even

Odd

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:
Even

Odd

Even

Odd
> ;
.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:

Even


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
The code is as follows:


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.

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
css中id选择符的标识是什么css中id选择符的标识是什么Sep 22, 2022 pm 03:57 PM

在css中,id选择符的标识是“#”,可以为标有特定id属性值的HTML元素指定特定的样式,语法结构“#ID值 {属性 : 属性值;}”。ID属性在整个页面中是唯一不可重复的;ID属性值不要以数字开头,数字开头的ID在Mozilla/Firefox浏览器中不起作用。

Vue3获取DOM节点的方式有哪些Vue3获取DOM节点的方式有哪些May 11, 2023 pm 04:55 PM

1.原生js获取DOM节点:document.querySelector(选择器)document.getElementById(id选择器)document.getElementsByClassName(class选择器)....2.vue2中获取当前组件的实例对象:因为每个vue的组件实例上,都包含一个$refs对象,里面存储着对应的DOM元素或组件的引用。所以在默认情况下,组件的$refs指向一个空对象。可以先在组件上加上ref="名字",然后通过this.$refs.

PHP中的DOM操作指南PHP中的DOM操作指南May 21, 2023 pm 04:01 PM

在网页开发中,DOM(DocumentObjectModel)是一个非常重要的概念。它可以让开发者轻松地对一个网页的HTML或XML文档进行修改和操作,比如添加、删除、修改元素等。而PHP中内置的DOM操作库也为开发者提供了丰富的功能,本文将介绍PHP中的DOM操作指南,希望可以帮助到大家。DOM的基本概念DOM是一个跨平台、独立于语言的API,它可以将

vue dom是什么意思啊vue dom是什么意思啊Dec 20, 2022 pm 08:41 PM

dom是一种文档对象模型,同时也是用于html编程的接口,通过dom来操作页面中的元素。DOM是HTML文档的内存中对象表示,它提供了使用JavaScript与网页交互的方式。DOM是节点的层次结构(或树),其中document节点作为根。

vue3中ref绑定dom或组件失败的原因是什么及怎么解决vue3中ref绑定dom或组件失败的原因是什么及怎么解决May 12, 2023 pm 01:28 PM

vue3ref绑定dom或者组件失败原因分析场景描述在vue3中经常用到使用ref绑定组件或者dom元素的情况,很多时候,明明使用ref绑定了相关组件,但是经常ref绑定失败的情况。ref绑定失败情况举例ref绑定失败的绝大多数情况是,在ref和组件绑定的时候,该组件还未渲染,所以绑定失败。或者组件刚开始未渲染,ref未绑定,当组件开始渲染,ref也开始绑定,但是ref和组件并未绑定完成,这个时候使用组件相关的方法就会出现问题。ref绑定的组件使用了v-if,或者他的父组件使用了v-if导致页面

css伪选择器学习之伪类选择器解析css伪选择器学习之伪类选择器解析Aug 03, 2022 am 11:26 AM

在之前的文章《css伪选择器学习之伪元素选择器解析​》中,我们学习了伪元素选择器,而今天我们详细了解一下伪类选择器,希望对大家有所帮助!

dom和bom对象有哪些dom和bom对象有哪些Nov 13, 2023 am 10:52 AM

dom和bom对象有:1、“document”、“element”、“Node”、“Event”和“Window”等5种DOM对象;2、“window”、“navigator”、“location”、“history”和“screen”等5种BOM对象。

javascript选择器失效怎么办javascript选择器失效怎么办Feb 10, 2023 am 10:15 AM

javascript选择器失效是因为代码不规范导致的,其解决办法:1、把引入的JS代码去掉,ID选择器方法即可有效;2、在引入“jquery.js”之前引入指定JS代码即可。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version