The study and simple application of JS beginner--this

Source: Internet
Author: User

<!DOCTYPE HTML><HTML>    <Head>        <MetaCharSet= "UTF-8">        <title></title>        <Scripttype= "Text/javascript">window.onload= function (){                varobtn=document.getElementById ("btn"); functionfn () {alert ( This);                } fn (); Obtn.onclick=fn; }        </Script>    </Head>    <Body>        <inputtype= "button"name= "BTN"ID= "BTN"value= "button" />    </Body></HTML>
View Code

This refers to the object that invokes the current method (function), or it can be understood as who let the function run, who is the this.

var obtn = document.getElementById ("btn"function() {                    this;                }

It is clear that the object calling the current anonymous function is OBTN

function () {                var obtn = document.getElementById ("btn");                 function fn () {                    this;                }                fn ();                 function () {                    fn ();                }            }

When another function is called in the Obtn, the FN object is still window.

Understand:

<script type= "Text/javascript" >window.onload=function (){                varABTN = document.getElementsByTagName ("Input");  for(vari = 0; i<abtn.length;i++) {Abtn[i].onclick=function(){                     This. Style.backgroundcolor = "Yellow"; }                }            }        </script> View Code

This refers to the input tag of the OBTN

 

<script type= "Text/javascript" >window.onload=function (){                varABTN = document.getElementsByTagName ("Input"); varthat =NULL;  for(vari = 0; i<abtn.length;i++) {Abtn[i].onclick=function() { that= This;                    FN (); }                }                functionfn () {That.style.backgroundColor= "Yellow"; }            }        </script> View Code

You can define an empty element by saving the object that corresponds to the current this to an empty element and then implementing it in another function.

<script type= "Text/javascript" >window.onload=function (){                varABTN = document.getElementsByTagName ("Input"); varthat =NULL;  for(vari = 0; i<abtn.length;i++) {Abtn[i].onclick=fn; }                functionfn () { This. Style.backgroundcolor = "Yellow"; }            }        </script> View Code

This is another use.

The study and simple application of JS beginner--this

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.