This tutorial introduces one of the most popular JavaScript frameworks today: MooTools. MooTools (for My Object Oriented Tools) is compatible with Internet Explorer 6, Firefox, Opera, and Chrome. MooTools is designed to be compact, modular and object-oriented. MooTools is designed for mid-to-high-level JavaScript programmers, so make sure you have enough experience before you dig deeper.
Core points
Get MooTools
Getting MooTools is very easy. MooTools are divided into two parts, called Core and More. Core contains core classes and modules of the framework, and more include additional classes that can be included according to application requirements. MooTools has a powerful builder page that customizes cores and more to our needs. The current stable version of MooTools core (at the time of writing) is 1.4.5 and more 1.4.0.1.
Element Selector
One of the most basic operations of any JavaScript framework is to select elements. MooTools uses Slick as its selector engine. MooTools supports many different types of selectors. This section describes some of the most useful and important selectors.Select element by ID
syntax. MooTools, like many other JavaScript frameworks, uses document.getElementById('id_of_element')
to shorten this operation. Your code ends up looking like this: $
var element = $('id_of_element');
as another way to select elements. The code you selected the element now looks like this: $
document.id()
var element = document.id('id_of_element');
var element = $('id_of_element');
The following example selects elements whose id and class attributes start with a specific pattern.
var element = document.id('id_of_element');
Similarly, the following example matches elements whose id and class attributes end in a specific pattern.
$$('.class_name').each(function(ele){ console.log(ele); });
DomReady Event
DomReady is an event that can only be bound to window objects. DomReady executes immediately after the DOM is loaded, which is probably much earlier than the window.load event that waits for all other resources to load. I suggest reading in-depth about DomReady and window.load comparison. The following example uses DomReady to wait for it to load before querying the DOM.
$$('[id^=id_start_]').each(function(ele){ console.log(ele); }); $$('[class^=class_start_]').each(function(ele){ console.log(ele); });
Element creation
MooTools can create new HTML elements and inject them into the DOM. Creating new HTML elements in MooTools is very simple. For example, the following code creates a new div element.
$$('[id$=_end_id]').each(function(ele){ console.log(ele); }); $$('[class$=_end_class]').each(function(ele){ console.log(ele); });
The previous code creates a new element but does not inject it into the DOM. To perform injection, you need to call the adopt()
method. The following example shows how to do this.
window.addEvent('domready', function(){ if(document.id('id_of_element')) { alert('Element Exists'); } });
When executing this code, you can see the added new div before ending the body tag.
Event binding
Event binding causes code to be executed when certain events are executed on elements. Click, double-click, and hover are examples of common events. You can also create your own custom events, but this is beyond the scope of this article. As an example of MooTools event binding, the following code adds a simple click event handler to an element.
var new_div = new Element('div', {'class': 'new_div'});
You can also add events to dynamically created elements. The following code adds the click handler to the dynamically created element.
var new_div = new Element('div', {'class': 'new_div'}); $$('body').adopt(new_div);
Browser detection
Last but not least is browser detection using MooTools. This is required when you want to write conditional code based on the user's browser. MooTools provides a Browser
object that is populated when the page is loaded. The following example uses a switch statement to identify the current browser.
document.id('id_of_ele').addEvent('click', function(){ console.log('I was just clicked'); });
Conclusion
This article introduces many basic knowledge of MooTools. There are many excellent resources to learn to use MooTools effectively. I learned a lot from the MooTools documentation and the David Walsh blog. You can also refer to my MooTools work.
(Second FAQ section can be rewrited similarly as needed, and the wording and sentence structure can be adjusted while maintaining consistency of the content)
The above is the detailed content of Getting Started with MooTools. For more information, please follow other related articles on the PHP Chinese website!