One week learn Mootools 1.4 Chinese tutorial :( 3) Events

Source: Internet
Author: User
Tags mootools

Today, we will explain the event part of mt. The event description mainly includes three parts: binding, removing, and triggering. Let's take a look at an example.

// Jquery event binding method
$ ('A'). click (function ){
Alert ('A ');
});
// Or
$ ('A'). bind ('click, mouseover', function ){
Alert ('A ');
});

// Event binding method of mt
$ ('A'). addEvent ('click', function ){
Alert ('A ');
});
Or
$ ('A'). addEvents ({
'Click': function ){
Alert ('A ');
},
'Mouseenter': function ){
Alert ('bb ');
}
});

From the above example, we can see that the jq event binding method is very similar to that of mt, because mt does not need to be encapsulated into function, therefore, we can also directly write events on the node, such:

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Script style = "text/javascript" src = "https://ajax.googleapis.com/ajax/libs/mootools/1.4.1/mootools-yui-compressed.js"> </script>
</Head>

<Body>
<Div id = 'A' onclick = "aa (this, 'B')"> click </div>
<Script type = 'text/javascript '>

Var aa = function (I, msg ){
Alert (I. get ('tag') + '|' + I. getProperty ('id') + '|' + msg );
}
</Script>

In the above example, I passed in the object itself, that is, this, and then I can understand that it has already selected a node, just operate on the node as you would.

Next we will mainly explain the first method. When using the first method, we must ensure that the dom node has been loaded. If the node has not been loaded due to network reasons, in this case, binding events will fail, so we can use the following method to avoid this problem:

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Script style = "text/javascript" src = "https://ajax.googleapis.com/ajax/libs/mootools/1.4.1/mootools-yui-compressed.js"> </script>
</Head>

<Body>
<Div id = 'A' onclick = "aa (this, 'B')"> click </div>
<Script type = 'text/javascript '>
Window. addEvent ('domainready', function (){
Alert ('execute First ');
});
Window. addEvent ('load', function (){
Alert ('executed later ');
});
</Script>

In the preceding example, the domready event is executed before onload. Note that onload is executed only after all dom nodes are loaded, therefore, domready has been executed before the dom node is loaded. load can only appear once on a page, while domready can be used multiple times. otherwise, IE will dislike you.
What we need to do now is bind an event to a after loading. Let's look at the example below:

<Body>
<A href = 'HTTP: // www.google.com 'id = 'A'> click </a>
<Script type = 'text/javascript '>
Window. addEvent ('load', function (){
Var aa = function (event ){
Event. stop ();
Alert ('aa1 ');
}
$ ('A'). addEvent ('click', aa );
});
</Script>
Or
<Body>
<A href = 'HTTP: // www.google.com 'id = 'A'> click </a>
<Script type = 'text/javascript '>
Window. addEvent ('load', function (){
$ ('A'). addEvent ('click', function (event ){
Event. stop ();
Alert ('aa1 ');
});
});
</Script>
If you can confirm that node a has been loaded, you can omit the load event, that is:
<Body>
<A href = 'HTTP: // www.google.com 'id = 'A'> click </a>
<Script type = 'text/javascript '>
$ ('A'). addEvent ('click', function (event ){
Event. stop ();
Alert(event.tar get); // object itself, the development plug-in is very useful
Alert (event. relatedTarget );
Alert (event. key); // returns the lowercase letter pressed.
Alert (event. shift); // returns true if the key pressed is shift.
Alert ('aa1 ');
});
</Script>

In the above example, in order to prevent a from being hyperlink to google, I passed the event parameter and used the event. stop () to block the default event. for more information about event parameters, see the following example:

$('I7 '). addEvent ('keypress', function (event ){
Alert (event. key );
Alert ('Code: '+ event. code); // The keyboard code of the buttons
Alert ('shift: '+ event. shift );
Alert ('control: '+ event. control );
Alert ('alt: '+ event. alt );
Alert ('meta: '+ event. meta );

// Ctr + S key combination
If (event. key ='s '& event. control ){
Alert ('document saved .');
}
});

 

So how to bind multiple events to an object? See the following example:

Var fun1 = function (){};
$ ('A'). addEvents ({
'Mouseenter': fun1,
'Mouseleave ': function (){}
});

Through the above example, we have already bound two events to the node a. Remember not to add a comma to the end of the last event, or IE will make an error.

How can I remove an event after it is bound? Let's look at the example.

Var destroy = function () {alert ('boom: '+ this. id );}
$ ('Myelement'). addEvent ('click', destroy );
$ ('Myelement'). removeEvent ('click', destroy );

The following is an example of event triggering:

Var txt = $('i7 ');
Txt. addEvents ({
'Focal ': function (){
If (txt. value. contains ('Type here ') txt. value = '';
},
'Keyup': function (){
If (txt. value. contains ('hello') {txt. fireEvent ('burn ', 'Hello world! ');}
Else if (txt. value. contains ('moo') {txt. fireEvent ('burn ', 'mootools! ');}
Else if (txt. value. contains ('22') {txt. fireEvent ('burn ', 'italy! ');}
Else if (txt. value. contains ('33') {txt. fireEvent ('burn ', 'fireevent ');}
Else if (txt. value. contains ('q') {txt. fireEvent ('burn ', "I'm a bit late! ", 1000 );}
},
'Burn ': function (text ){
Alert (text + '|' + txt. value );
Txt. value = '';
}
});


Some common event names are listed below. Of course, mt allows us to customize events. If you are interested, you can study how to customize events:

Domready
Load
Unload
Beforeunload
Selectstart
Selectend
Keypress
Blur
Change
Click
Dblclick
Focus
Focusin
Focusout
Keydown
Keypress
Keyup
Keyup
ScrollTo: scroll
Scroll: Rolling
Resize: When the size is changed
Move
Reset
Submit
Error
Abort
Mousemove
Mouseout
Mouseover
Mouseup
Mousedown
Mouseenter: When the mouse enters, make up for the mouseover problem.
Mouseleave: After the mouse leaves
Mousewheel: After scrolling
Contextmenu: Right-click
DOMMouseScroll
DOMContentLoaded
Readystatechange

(Source: http://see7di.cnblogs.com)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.