Two good ways to get an event source in a JavaScript event function

Source: Internet
Author: User

In many cases we need to get the event source object to make changes to its properties, and there are two ways to get the event source in the event response function

In the JavaScript event response, there are many situations where we need to get the event source object to make changes to its properties, such as changing the SRC attribute of IMG. There are two ways to get event sources in an event response function: The first: directly passing as a parameter. For example, the code is as follows: <div id= "myID" onclick= "show (this);"  >text</div> <script type= "Text/javascript" > function show (obj) {window.alert (obj.id); </script> Second: Drop the hidden event object directly. The event object has a Srcelement property that can be accessed directly by its code as follows: <div id= "myID" onclick= "Show ();"  >text</div> <script type= "Text/javascript" > Function Show () {Window.alert (event.srcElement.id); } </script>
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.