Based on the difference between this and event in js (detailed description), jsevent

Source: Internet
Author: User

Based on the difference between this and event in js (detailed description), jsevent

Today, you can see this and event parameters in the event chapter, a typical introduction to javascript. As a beginner front-end developer, I only use this to pass parameters. so I 'd like to find out what is the difference between this and event, and what is appropriate under what circumstances.

Onclick = changeImg (this) vs onclick = changeImg (event)

<script>  var myImages = [    'usa.gif','canada.gif','jamaica.gif','mexico.gif'  ];  function changeImg(e) {    var el = e.target;    var newImgNumber = Math.round(Math.round()*3);    while(el.src.indexOf(myImages[newImgNumber]) != -1){      el.src =myImages[newImgNumber];    }  }</script>

1. this is a keyword in Javascript.

2. this indicates an internal object automatically generated when the function is running. It can only be used inside the function.

3.thisvs event.tar get:

Events in JSS are fraudulent, so thiscan be changed, but event.tar get does not change (when an event is triggered, only the reference of the current event object is passed). It is always the target DOM element that directly accepts the event;

In addition, thisand event.tar get are dom objects. If you want to use methods in jquey, you can convert them to jquery objects: sums (this(and counts (event.tar get );

Based on the difference between this and event in js (detailed description), this is all the content shared by Alibaba Cloud xiaobian. I hope you can provide a reference and support for the customer's house.

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.