Js creates an input array and binds the click event.

Source: Internet
Author: User

Copy codeThe Code is as follows:
</Pre> <pre name = "code" class = "javascript"> <Body>
<Input type = "button" name = "input []" value = "button 1"/> <br/>
<Input type = "button" name = "input []" value = "button 2"/> <br/>
<Input type = "button" name = "input []" value = "button 3"/> <br/>

<Div id = "add"> </div>
</Body>
</Html>

<Script type = "text/javascript">

// The input control is obtained through getElementsByTagName.
Var inputs = document. getElementsByTagName ("input ");
// Bind The onclick event to 0th buttons. Click alert.
Inputs [0]. onclick = function (){
Alert ("let me test ");
}

// Bind an onclick event to each button. Click alert.
For (var I = 0; I <inputs. length; I ++ ){
Inputs [I]. onclick = function (){
Alert ("let me test ");
}
}

Window. onload = function (){
// Define an array arrs
Var arrs = new Array ();
// Cyclically add
For (var I = 0; I <2; I ++ ){
// Cyclically add two input types = "button" value = "add" + I
Var input = document. createElement ("input ");
Input. type = "button ";
Input. value = "add" + I;
// Remember to put the created input into arrs
Arrs. push (input );
// Put the input in the div id = "add"
Document. getElementById ("add"). appendChild (input );
}

// Bind the event with the same [0]. onclick. the problem persists.
Arrs [0]. onclick = function (){
Alert ("test again ");
}

}
</Script>

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.