用for循环来添加onmouseover、onmouseout等事件的时候
怎样才能获取当前元素的序号 例如点击第2行的时候 第2行的循环序号i应该是1 但是获取的结果却是循环结束时i的值5
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>JS touch show</title>
<style type="text/css">
.eee{ background:#ccc;}
.on{ background:red}
.click{ background:#333;color:#fff;}
li{width:100px;height:20px;background:black;margin-top:5px;}
</style>
<body>
<table width="100%" border="0" cellspacing="0">
<tr>
<td>111111111111111111111111111</td>
</tr>
<tr>
<td>222222222222222222222</td>
</tr>
<tr>
<td>3333333333333333</td>
</tr>
<tr>
<td>444444444444444444444</td>
</tr>
<tr>
<td>5555555555555</td>
</tr>
</table>
<ul id="aa">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
<script language="javascript">
window.onload=function(){
var trs = document.getElementsByTagName('tr'),cur=trs[0];
var lis=document.getElementsByTagName('li');
for(var i=0; i<trs.length; i++){
trs[i].setAttribute('num',i);
trs[i].setAttribute('on','false');
trs[i].className='eee';
trs[i].onclick=function(){
var a = this.getAttribute('on');
cur.className='eee';
cur.setAttribute('on','false');
(cur=this).setAttribute('on','true');
(cur=this).className = 'click';
alert(i);
}
trs[i].onmouseover=function(){
this.className=='click' ?'':this.className = 'on';
}
trs[i].onmouseout=function(){
this.className=='click'?'':this.className='eee';
}
}
}
</script>
</body>
</html>
怎样才能获取当前元素的序号 例如点击第2行的时候 第2行的循环序号i应该是1 但是获取的结果却是循环结束时i的值5
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>JS touch show</title>
<style type="text/css">
.eee{ background:#ccc;}
.on{ background:red}
.click{ background:#333;color:#fff;}
li{width:100px;height:20px;background:black;margin-top:5px;}
</style>
<body>
<table width="100%" border="0" cellspacing="0">
<tr>
<td>111111111111111111111111111</td>
</tr>
<tr>
<td>222222222222222222222</td>
</tr>
<tr>
<td>3333333333333333</td>
</tr>
<tr>
<td>444444444444444444444</td>
</tr>
<tr>
<td>5555555555555</td>
</tr>
</table>
<ul id="aa">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
<script language="javascript">
window.onload=function(){
var trs = document.getElementsByTagName('tr'),cur=trs[0];
var lis=document.getElementsByTagName('li');
for(var i=0; i<trs.length; i++){
trs[i].setAttribute('num',i);
trs[i].setAttribute('on','false');
trs[i].className='eee';
trs[i].onclick=function(){
var a = this.getAttribute('on');
cur.className='eee';
cur.setAttribute('on','false');
(cur=this).setAttribute('on','true');
(cur=this).className = 'click';
alert(i);
}
trs[i].onmouseover=function(){
this.className=='click' ?'':this.className = 'on';
}
trs[i].onmouseout=function(){
this.className=='click'?'':this.className='eee';
}
}
}
</script>
</body>
</html>
var trs = document.getElementsByTagName('tr'),cur=trs[0];
var lis=document.getElementsByTagName('li');
for(var i=0; i<trs.length; i++){
trs[i].setAttribute('num',i);
trs[i].setAttribute('on','false');
trs[i].className='eee';
trs[i].onclick=function(){
var a = this.getAttribute('on');
cur.className='eee';
cur.setAttribute('on','false');
(cur=this).setAttribute('on','true');
(cur=this).className = 'click';
alert(event.srcElement.getAttribute("num")); }
trs[i].onmouseover=function(){
this.className=='click' ?'':this.className = 'on';
}
trs[i].onmouseout=function(){
this.className=='click'?'':this.className='eee';
}
}
}
这里有操作table对象的文章。。你一看便知道了
实际用的时候也是不需要getAttribute('num')这个属性的
除了另设一个属性外 难道没有别的方法取到当前的序号
当调用onclick事件时,i的值已经变成循环后的最后一个值了,也就是5。解决的方法很简单,创建一个闭包就可以: for(var i=0; i<trs.length; i++){
trs[i].setAttribute('num',i);
trs[i].setAttribute('on','false');
trs[i].className='eee';
(function(m){//此处接收传入的i值,不是引用
trs[m].onclick=function(){
var a = this.getAttribute('on');
cur.className='eee';
cur.setAttribute('on','false');
(cur=this).setAttribute('on','true');
(cur=this).className = 'click';
alert(m);
}
})(i);//此处将i当作数值传入匿名函数
……