Test onclick addEventListener for event binding

Source: Internet
Author: User

The development tool should have corresponding functions, so it is tested:
Premise: it is only a simple small test, and addEventListener is a standard binding function. IE is incompatible with this function (IE corresponds to attachEvent). Therefore, this test first discards IE and uses Firefox 5.0.1, chrome 14.0, Opera 11.50
Test page: Copy codeThe Code is as follows: <! DOCTYPE html>
<Html>
<Head>
<Title> Test </title>
<Style type = "text/css">
. Test {
Background-color: # FFF;
Border: 1px solid gray;
Height: 100px;
Width: 100px;
Margin: 0 10px 0;
Float: left;
}
</Style>
</Head>
<Body>
<Div id = "test1" class = "test" onclick = "console. log ('test1: click! '); "Onmouseover =" console. log ('test1: mouseover! '); "> TEST1 </div>
<Div id = "test2" class = "test"> TEST2 </div>
<Div id = "test3" class = "test"> TEST3 </div>
<Script type = "text/javascript">
(Function (){
Var $ = function (o) {// Simple Tool
Return document. getElementById (o );
}
// For Test2:
$ ('Test2'). onclick = function () {console. log ('test2: click! ');}
$ ('Test2'). onmouseover = function () {console. log ('test2: mouseover! ');}
// For Test3:
$ ('Test3'). addEventListener ('click', function (){
Console. log ('test3: click! ');
}, False );
$ ('Test3'). addEventListener ('mouseover', function (){
Console. log ('test3: mouseover! ');
}, False );
})();
</Script>
</Body>
</Html>

The page effect is as follows:

Three most common event binding methods are used in the test code.

FIREFOX:

1. Select Firebug in FirefoxTest1Element:

All attributes are displayed in the right DOM Tag:

The attributes array is found as follows:

This is because the two event handles of the test1 element are directly written to the element, and more related information cannot be found in the list, in this binding mode, the event handle can only be found in the attributes of the firebug DOM tag.

2. SelectTest2Element:

Right DOM Tag:

Test2 binds the event handle in javascript and is displayed as a "user-defined property" in the DOM tag. Right-click the tag to view the relevant information:

3. SelectTest3Element:

This time, no information about the event bound to this element is found in the DOM tag...

All in all, in firebugUnder the standard binding event Function(AddEventListener), it is not good to detect the event function bound to an element. (do not try to print the onclick attribute of that element. addEventListener binding and onclick binding are irrelevant, in this case, you will get nudefined); If yesBind the first two eventsTo obtain some simple information.

-----

OPERA:

1. SelectTest1:

You can find the relevant information in the attribute list on the right:

Click the plus sign to view the bound function information:

To view a function body, you can enter the following in the console:

Print the function body directly, making it more convenient!

2. SelectTest2AndTest1Almost the same, so I will not go into details;

3. SelectTest3:

The attribute tag on the right cannot be found. The values of onclick and onmouseover are null.

All in all, Opera Dragonfly is similar to Firefox Firebug, and it is not clear about the events bound to an element in the standard binding function.

-----

CHROME:

Chrome is much clearer:

1. SelectTest1 (or test2, the two are similar)

Check the information bit on the right:

In ChromeEvent ListenersIt is a good tool to list the listener functions already bound to the selected element. Click the black triangle to view the bound function information:

There are two items in "click". The first item is div # test1, which is the information of the onclick function we bound. The document of the second item can be ignored (Chrome's own processing );

IsAttribute: true:This onclick function handle is treated as an element attribute (because we use onclick = function (){...}, Whether it is test1 or test2 );

LineNumber: 18:Location of the bound function;

UseCapture: false:It indicates that event capture is not used. If other features have strong semantics, they will not be used;

SimilarlyPropertiesIn the first HTMLDivElement list of, both test1 and test2 can be seen:

2. SelectTest3:

In Properties, both onclick and onmouseover are null, but the Event Listeners is still:

But note the div # test3:

IsAttribute is set to false because the onclick attribute is not used, but the addEventListener is used.

Remember this first! A bit messy ~

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.