js设置元素的onclick传参方法
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
- <HTML>
- <HEAD>
- <TITLE> New Document </TITLE>
- <META NAME="Generator" CONTENT="EditPlus">
- <META NAME="Author" CONTENT="">
- <META NAME="Keywords" CONTENT="">
- <META NAME="Description" CONTENT="">
- <script>
- var array=[1,2,3,4,5,6];
- function testvalue(val)
- {
- alert(val);
- }
- function add(){
- for(i=0,len=array.length;i<len;i++)
- {
- var newOption = document.createElement("li");
- newOption.innerHTML=i+1;
- newOption.onclick=function (val){ return function(){testvalue(val);}}(array[i])
- document.getElementById('div').appendChild(newOption);
- }
- }
- </script>
- </HEAD>
- <BODY>
- <input type='button' value='click me' onclick='add()' />
- <div id='div'></div>
- </BODY>
- </HTML>
- <!DOCTYPE HTML>
- <html>
- <head>
- <script type="text/javascript">
- </script>
- </head>
- <body>
- <div id="showDiv">
- this is showDiv
- </div>
- </body>
- <script type="text/javascript">
- var arr = new Array("a","b","c");
- funcA();
- function funcA(){
- var str = "";
- for(var m=0;m<arr.length;m++){
- str +=arr[m];
- var a = document.createElement("A");
- var textStr = document.createTextNode("链接到"+m);
- a.appendChild(textStr);
- a.href="javascript:void(0)";
- a.onclick=function (val){ return function(){funcB(val);}}(m);
- var s = document.getElementById("showDiv");
- s.appendChild(a);
- }
- }
- function funcB(m){
- alert("打开超链接"+m);
- }
- </script>
- </html>