Example of JavaScript anonymous function usage in the Loop Structure

Source: Internet
Author: User

JavaScript Functions are worth learning. This article will focus on the use of JavaScript anonymous functions in the loop structure. I believe you will be interested.

JavaScript anonymous functions in the Loop Structure

First look at the following code

 
 
  1. <SCRIPTLANGUAGESCRIPTLANGUAGE = "JavaScript">
  2. <! --
  3. FunctionDelete_Row (I)
  4. {
  5. Alert (I );
  6. }
  7. Functiontest ()
  8. {
  9. For (vari = 0; I <5; I ++)
  10. {
  11. Vartable11 = document. getElementById ("table11 ");
  12. Vartr11 = table11.insertRow ();
  13. Vartrstr = "tr" + tr11.rowIndex;
  14. Tr11.id = trstr;
  15. Tr11.ondblclick = function ()
  16. {
  17. Delete_Row (trstr );
  18. };
  19. Vartd11 = tr11.insertCell ();
  20. Td11.innerHTML = I;
  21. Td11 = tr11.insertCell ();
  22. Td11.innerText = "I Love You ";
  23. Td11 = tr11.insertCell ();
  24. Td11.innerText = "dumb ";
  25. Td11 = tr11.insertCell ();
  26. }
  27. }
  28. // -->
  29. </SCRIPT>
  30. <Tableidtableid = "table11" border = 1>
  31. <Tbody>
  32. <Tr>
  33. <Td> first column </td>
  34. <Td> second column </td>
  35. <Td> third column </td>
  36. </Tr>
  37. </Tbody>
  38. </Table>
  39. <Br>
  40. <Inputtypeinputtype = "button" value = "GO" onclick = "test ()">
  41.  

After you click the button, five rows are inserted in Table 11. The double-click event of a row uses the JavaScript anonymous function.

Imagine what you will see when you click row 2nd and row 3rd. Will it be alert ('tr0') and alert ('tr1'), just like what you think?

Apparently it wasn't the result I wanted, but alert ('tr5') popped up. It was a bit strange! It is certain that the problem lies in the JavaScript anonymous function. It may be that the double-click event of this row points to the same JavaScript anonymous function. I don't know what you think?

If you change the above Code to the following code, you will get the expected result.

 
 
  1. <SCRIPTLANGUAGESCRIPTLANGUAGE = "JavaScript">
  2. <! --
  3. FunctionDelete_Row (I)
  4. {
  5. Alert (I );
  6. }
  7. Functiontest ()
  8. {
  9. For (vari = 0; I <5; I ++)
  10. {
  11. Vartable11 = document. getElementById ("table11 ");
  12. Vartr11 = table11.insertRow ();
  13. Vartrstr = "tr" + tr11.rowIndex;
  14. Tr11.id = trstr;
  15. Tr11.ondblclick = f (trstr );
  16. Vartd11 = tr11.insertCell ();
  17. Td11.innerHTML = I;
  18. Td11 = tr11.insertCell ();
  19. Td11.innerText = "I Love You ";
  20. Td11 = tr11.insertCell ();
  21. Td11.innerText = "dumb ";
  22. Td11 = tr11.insertCell ();
  23. }
  24. }
  25. Functionf (I)
  26. {
  27. Return function ()
  28. {
  29. Delete_Row (I );
  30. }
  31. }
  32. // -->
  33. </SCRIPT>
  34. <Tableidtableid = "table11" border = 1>
  35. <Tbody>
  36. <Tr>
  37. <Td> first column </td>
  38. <Td> second column </td>
  39. <Td> third column </td>
  40. </Tr>
  41. </Tbody>
  42. </Table>
  43. <Br>
  44. <Inputtypeinputtype = "button" value = "GO" onclick = "test ()">
  45.  

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.