The onclick parameter method of JS set element

Source: Internet
Author: User

  1. <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en">
  2. <HTML>
  3. <HEAD>
  4. <title> New Document </title>
  5. <META name= "Generator" content="EditPlus">
  6. <META name= "Author" content= "">
  7. <META name= "Keywords" content= "">
  8. <META name= "Description" content= "">
  9. <script>
  10. var array=[1,2,3,4,5,6];
  11. Function TestValue (val)
  12. {
  13. Alert (val);
  14. }
  15. function Add () {
  16. For (i=0,len=array.length;i<len;i++)
  17. {
  18. var newoption = document.createelement ("Li");
  19. newoption.innerhtml=i+1;
  20. newoption.onclick=Function (val) {return function () {TestValue (val);}} (Array[i])
  21. document.getElementById (' div '). appendchild (newoption);
  22. }
  23. }
  24. </Script>
  25. </HEAD>
  26. <BODY>
  27. <input type=' button ' value=' Click me ' onclick=' Add () ' />
  28. <div id=' div '></div>
  29. </BODY>
  30. </HTML>

[HTML]View Plaincopy
  1. <! DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <script type="Text/javascript">
  5. </Script>
  6. </head>
  7. <body>
  8. <div id="Showdiv">
  9. This is Showdiv
  10. </div>
  11. </body>
  12. <script type="Text/javascript">
  13. var arr = new Array ("A", "B", "C");
  14. Funca ();
  15. function Funca () {
  16. var str = "";
  17. for (Var m=0;m<arr.length;m++) {
  18. STR +=arr[m];
  19. var a = document.createelement ("a");
  20. var textstr = document.createtextnode ("link to" +m);
  21. A.appendchild (TEXTSTR);
  22. a.href="javascript:void (0)";
  23. a.onclick=Function (val) {return function () {FUNCB (val);}}  (m);
  24. var s = document.getElementById ("Showdiv");
  25. S.appendchild (a);
  26. }
  27. }
  28. function FUNCB (m) {
  29. Alert ("Open HYPERLINK" +m);
  30. }
  31. </Script>
  32. </html>

JS settings element's onclick parameter method

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.