Example of using closures

Source: Internet
Author: User

<Div>
<Input id = "one" type = "button" value = "button 1"/>
<Input id = "two" type = "button" value = "button 2"/>
<Input id = "three" type = "button" value = "button 3"/>
<Button onclick = location. Reload ()> Reset </button>
</Div>

<SCRIPT>
Function click_times (){
For (VAR I = 1; I ++ ){
Return function (){
Alert (I ++)
}
}
}
VaR temp = [];

VaR node = Document. getelementsbytagname ('input ');
For (VAR q = 0; q <node. length; q ++ ){
Temp. Push (click_times ());
Node [Q]. addeventlistener ('click', temp [Q])
}


</SCRIPT>

I haven't posted any blog posts yet, but I don't know what the effect is. This code is not complicated. People who know the closure can understand it all. The goal is to click the button and the numbers in the pop-up box will increase one by one, the difference is that the three buttons do not affect each other. I put the method in the array to be traversed.

Example of using closures

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.