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

Actan 2009-11-16 04:53:35

<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
...全文
989 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
C_Bull 2012-10-16
  • 打赏
  • 举报
回复
不好意思,当时不知道脑子坏了哪根弦了,发了那么个帖子。表示道歉……
Actan 2011-02-12
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 c_bull 的回复:]

引用 1 楼 zswang 的回复:
参考如下代码:

HTML code
<html>
<head>
<script language="javascript">
function hello222(){
for (var i=1; i<3; i++){
var cell = document.getElementById("cell_"+i);
cell.onclick………
[/Quote]

你确定没反应?他的代码能运行啊,无错
C_Bull 2010-11-04
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 zswang 的回复:]
参考如下代码:

HTML code
<html>
<head>
<script language="javascript">
function hello222(){
for (var i=1; i<3; i++){
var cell = document.getElementById("cell_"+i);
cell.onclick……
[/Quote]
得分的那个 我复制了你代码 到本地后点了根本没反应,这是为什么?我给你个“板砖”……
Jeffrey84 2009-11-16
  • 打赏
  • 举报
回复
搞一个闭包出来就可以了
l3385_2463_4984 2009-11-16
  • 打赏
  • 举报
回复
把你的hello222改为
function hello222(){
for (var i=1; i<3; i++){
var cell = document.getElementById("cell_"+i);
cellOnclick(cell,i)

}

function cellOnclick(el,i){
el.onclick= function () {
alert ('Removed 000 !! in goodbye!!!' + " " + i );
}
}
}
Actan 2009-11-16
  • 打赏
  • 举报
回复
对高手来说太easy了
哈哈
早点结了吧,不然这点分怕不够分哪

多谢!
王集鹄 2009-11-16
  • 打赏
  • 举报
回复
参考如下代码:
<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>

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧