日期:2014-05-16  浏览次数:20383 次

div.onclick = function (){传参数}??
HTML code

<html>
  2 <head>
  3 <script language="javascript">
  4 function hello222(){
  5     for (var i=1; i<3; i++){
  6         var cell = document.getElementById("cell_"+i);  
  7         cell.onclick= function (i) {alert ('Removed 000 !! in goodbye!!!' + "  " + i );}    
  8     }
  9 }
 10 
 11 function hello(){
 12     alert ("old on click!!");
 13 }
 14 
 15 
 16 function goodbye(passed){
 17     alert ('Removed !! in goodbye!!!' + "  " + passed );
 18 }
 19 </script>
 20 </head>
 21 <body>
 22 <a onclick="hello()" id="cell_1">me1</a>
 23 <a onclick="hello()" id="cell_2">me2</a>
 24 <p>
 25 <input type="button" value="remove onclick" onclick="hello222()">
 26 </body>
 27 </html>



需要在IE里面动态把目标的onclick事件批量更改。
同时要求传一个参数给更改后的onclick的事件函数。
比如更改前是这样:
<a onclick="hello()" id="cell_1">me1</a>
<a onclick="hello()" id="cell_2">me2</a>
要求更改后是这样:
<a onclick="hello_new('11')" id="cell_1">me1</a>
<a onclick="hello_new('22')" id="cell_2">me2</a>

参数怎么传呢?
我上面的代码传来传去只能得到一个同样的值: 3

------解决方案--------------------
参考如下代码:
HTML code
<html>
<head>
<script language="javascript">
function hello222(){
    for (var i=1; i<3; i++){
        var cell = document.getElementById("cell_"+i);  
        cell.onclick= new Function("hello_new('" + i + "');");
    }
}

function hello(){
    alert("old on click!!");
}


function hello_new(passed){
    alert('Removed !! in goodbye!!!' + "  " + passed );
}
</script>
</head>
<body>
<a onclick="hello()" id="cell_1">me1</a>
<a onclick="hello()" id="cell_2">me2</a>
<p>
<input type="button" value="remove onclick" onclick="hello222()">
</body>
</html>