Three ways to specify events for an element in JavaScript _javascript tips

Source: Internet
Author: User
Tags lowercase

In JavaScript, you can specify an event for an element in the following three ways:
1, in HTML, using the OnClick property
2, in JavaScript, using the OnClick property
3. In Javascipt, use the Addevenlistener () method

Comparison of three methods
(1) in the second to third method, you can pass in an event object to the function and read its corresponding property, and method one is not available.
(2) Preferred second to third, the first is not conducive to the separation of content and events, you can not use the event object related content.

Some grammatical details
(1) In the first method, the OnClick case is not relevant, but in the second method, you must use lowercase. Because HMTL is not sensitive to case, JS is case-sensitive.
(2) in the second to third method, there is no double quotation mark when specifying the function name, and the first one as an HTML property requires double quotes.
(3) The first method requires parentheses, and the second to third is not required.

Onclick= "ClickHandler ()"
document.getElementById ("Jsonclick"). onclick = ClickHandler2; 
document.getElementById ("AddEventListener"). AddEventListener ("click", ClickHandler2);

The complete code is as follows:

<! DOCTYPE html> 
 
 

In JavaScript, you can specify an event for an element in the following three ways:
1, in HTML, using the OnClick property

2, in JavaScript, using the OnClick property
(1) Note that the function name does not have double quotes.

3. In Javascipt, use the Addevenlistener () method

Comparison of three methods
(1) in the second to third method, you can pass in an event object to the function and read its corresponding property, and method one is not available.

Some grammatical details
(1) In the first method, the OnClick case is not relevant, but in the second method, you must use lowercase. Because HMTL is not sensitive to case, JS is case-sensitive.
(2) in the second to third method, there is no double quotation mark when specifying the function name, and the first one as an HTML property requires double quotes.
(3) The first method requires parentheses, and the second to third is not required.

Onclick= "ClickHandler ()"
document.getElementById ("Jsonclick"). onclick = ClickHandler2; 
document.getElementById ("AddEventListener"). AddEventListener ("click", ClickHandler2);

The complete code is as follows:

 <! DOCTYPE html>  
 
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.