<script type="text/javascript">
var oldobj=false;
function bg(obj){
if(oldobj){oldobj.style.backgroundColor="";}
obj.style.backgroundColor="#9966CC";
oldobj=obj;}
</script>
<table width="500" border="1" align="center">
<tr>
<td align="center"><a href="www.baidu.com" onclick="bg(this);return false">1</a></td>
<td align="center"><a href="www.baidu.com" onclick="bg(this);return false">1</a></td>
<td align="center"><a href="www.baidu.com" onclick="bg(this);return false">1</a></td>
<td align="center"><a href="www.baidu.com" onclick="bg(this);return false">1</a></td>
<td align="center"><a href="www.baidu.com" onclick="bg(this);return false">1</a></td>
</tr>
</table>
我怎么理解不了呢 oldobj代表其他没都点击的?
为什么没有if(oldobj) 这句 oldobj.style.backgroundColor就说oldobj不是对象
设置var oldobj=false;干什么用的?大家给我讲讲原理吧
var oldobj=false;
function bg(obj){
if(oldobj){oldobj.style.backgroundColor="";}
obj.style.backgroundColor="#9966CC";
oldobj=obj;}
</script>
<table width="500" border="1" align="center">
<tr>
<td align="center"><a href="www.baidu.com" onclick="bg(this);return false">1</a></td>
<td align="center"><a href="www.baidu.com" onclick="bg(this);return false">1</a></td>
<td align="center"><a href="www.baidu.com" onclick="bg(this);return false">1</a></td>
<td align="center"><a href="www.baidu.com" onclick="bg(this);return false">1</a></td>
<td align="center"><a href="www.baidu.com" onclick="bg(this);return false">1</a></td>
</tr>
</table>
我怎么理解不了呢 oldobj代表其他没都点击的?
为什么没有if(oldobj) 这句 oldobj.style.backgroundColor就说oldobj不是对象
设置var oldobj=false;干什么用的?大家给我讲讲原理吧
function bg(obj){
if(oldobj){oldobj.style.backgroundColor="";}//由于初始化是false所以第一次不进这里.
obj.style.backgroundColor="#9966CC";
oldobj=obj; //将当前对象赋给oldobj,所以以后oldobj就是一个DOM控件实例,所以if(oldobj)是真的.再次进入此函数的时候.就会将oldobj所指的DOM对像的底色置空.} 因为JS是弱数据类型.所以可以这么布尔类型和DOM对象混用.
所以第一次执行方法bg时,会报错oldobj不是对象。设置var oldobj=false;就是为了第一次执行方法bg时不执行if(oldobj){oldobj.style.backgroundColor="";} ,oldobj=obj; 将第1次点击的DOM对象赋给了oldobj,第二次执行方法bg时,
就会执行if(oldobj){oldobj.style.backgroundColor="";} ,
将第1次点击的DOM对象a的背景色设成无。obj.style.backgroundColor="#9966CC"; 将当前的DOM对象a的背景色设成="#9966CC
<script type="text/javascript">
var oldobj=false;//点击对象寄存器
//or:
//var oldobj
function bg(obj){
//////////////
alert(oldobj.innerHTML);
//////////////
if(oldobj){oldobj.style.backgroundColor="";}
obj.style.backgroundColor="#9966CC";
oldobj=obj; }
</script>
<table width="500" border="1" align="center">
<tr>
<td align="center"> <a href="www.baidu.com" onclick="bg(this);return false">1 </a> </td>
<td align="center"> <a href="www.baidu.com" onclick="bg(this);return false">2 </a> </td>
<td align="center"> <a href="www.baidu.com" onclick="bg(this);return false">3 </a> </td>
<td align="center"> <a href="www.baidu.com" onclick="bg(this);return false">4 </a> </td>
<td align="center"> <a href="www.baidu.com" onclick="bg(this);return false">5 </a> </td>
</tr>
</table>
<script type="text/javascript">
var oldobj=false;
function bg(obj){
if(oldobj){oldobj.style.backgroundColor="";}
obj.style.backgroundColor="#9966CC";
oldobj=obj;
}
</script>
<table width="500" border="1" align="center">
<tr>
<td align="center"><a href="www.baidu.com" onclick="bg(this);return false" style="background-color:#9966CC">1</a></td>
<td align="center"><a href="www.baidu.com" onclick="bg(this);return false">1</a></td>
<td align="center"><a href="www.baidu.com" onclick="bg(this);return false">1</a></td>
<td align="center"><a href="www.baidu.com" onclick="bg(this);return false">1</a></td>
<td align="center"><a href="www.baidu.com" onclick="bg(this);return false">1</a></td>
</tr>
</table>
如果设定第一个背景了 那么 怎么在点击别的时候清除第一个的背景?