<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
  <TITLE> New Document </TITLE>
  <SCRIPT LANGUAGE="JavaScript">
  <!--
var strM = ' <div align="left"> <em> <strong>ed </strong> </em>';
var c=[]
alert(strM.replace(/(<[^>]*?>)/g,function(a,b){c.push(b);return a;})); 
alert(c)
  </SCRIPT></HEAD><script>
function show(){
for(i=1;i <=5;i++)
  {
  var obj=document.createElement("A");
  var data=document.createTextNode(i);
  obj.id="i";
  obj.href="#";
  obj.onmousedown=(function (a){ return function(){alert(a);}})(i)
    obj.style.width="40";
  obj.appendChild(data);
  document.body.insertAdjacentElement("beforeEnd",obj);
  }
 }
</script>
<body onLoad="show()">
</body>
</HTML>new Function("")
也可以查查闭包

解决方案 »

  1.   

    function show(){ 
    for(i=1;i <=5;i++) 
      { 
      var obj=document.createElement("A"); 
      var data=document.createTextNode(i); 
      obj.id=i
      obj.href="#"; 
      obj.onmousedown=function(){ 
    alert(this.id); 
      }; 
      obj.style.width="40"; 
      obj.appendChild(data); 
      document.body.insertAdjacentElement("beforeEnd",obj); 
      } 
      } 
      

  2.   

    <body onLoad="show()"> 
    <script> 
    function show(){ 
    for(i=1;i <=5;i++) 
      { 
      var obj=document.createElement("A"); 
      var data=document.createTextNode(i); 
      obj.id="i"; 
      obj.href="#";
      obj.num = i; 
      obj.onmousedown=function(){ 
    alert(this.num); 
      }; 
      obj.style.width="40"; 
      obj.appendChild(data); 
      document.body.insertAdjacentElement("beforeEnd",obj); 
      } 
      } 
    </script> 
    </body>