<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Demo_For_Wang</title><script type="text/javascript">
function changeColor(){
//取获取table的对象
var t=document.getElementById("table1").childNodes.item(0);
//遍历table每一个单元格,将其背景色清空(事实上是设置为白色)
for(var i=0;i< t.childNodes.length;i++)
{
for(var j=0;j<t.childNodes(i).childNodes.length;j++)
{
t.childNodes(i).childNodes(j).bgColor="white";
}
}
//获取点击的超链接所在的单元格对象
var cell = event.srcElement.parentElement;
//设置背景为红色
cell.bgColor = "red";
}</script></head><body>
<table border="1px" id="table1">
<tr>
<td id="td1">
<a href="#" target="_blank" onclick="changeColor()">google</a>
</td>
<td id="td2">
<a href="#" target="_blank" onclick="changeColor()">baidu</a>
</td>
<td id="td3">
<a href="#" target="_blank" onclick="changeColor()">douban</a>
</td>
<td id="td4">
<a href="#" target="_blank" onclick="changeColor()">renren</a>
</td>
</tr>
</table></body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Demo_For_Wang</title><script type="text/javascript">
function changeColor(){
//取获取table的对象
var t=document.getElementById("table1").childNodes.item(0);
//遍历table每一个单元格,将其背景色清空(事实上是设置为白色)
for(var i=0;i< t.childNodes.length;i++)
{
for(var j=0;j<t.childNodes(i).childNodes.length;j++)
{
t.childNodes(i).childNodes(j).bgColor="white";
}
}
//获取点击的超链接所在的单元格对象
var cell = event.srcElement.parentElement;
//设置背景为红色
cell.bgColor = "red";
}</script></head><body>
<table border="1px" id="table1">
<tr>
<td id="td1">
<a href="javascript:changeColor('google')" target="_blank">google</a>
</td>
<td id="td2">
<a href="javascript:changeColor('baidu')" target="_blank" >baidu</a>
</td>
<td id="td3">
<a href="javascript:changeColor('douban')" target="_blank" >douban</a>
</td>
<td id="td4">
<a href="javascript:changeColor('renren')" target="_blank" >renren</a>
</td>
</tr>
</table></body>
</html>为什么下面的程序不合适啊? <a href="#" onclick="js()"> 与 <a href="javascript:js()">的区别是什么啊?
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Demo_For_Wang</title><script type="text/javascript">
function changeColor(){
//取获取table的对象
var t=document.getElementById("table1").childNodes.item(0);
//遍历table每一个单元格,将其背景色清空(事实上是设置为白色)
for(var i=0;i< t.childNodes.length;i++)
{
for(var j=0;j<t.childNodes(i).childNodes.length;j++)
{
t.childNodes(i).childNodes(j).bgColor="white";
}
}
//获取点击的超链接所在的单元格对象
var cell = event.srcElement.parentElement;
//设置背景为红色
cell.bgColor = "red";
}</script></head><body>
<table border="1px" id="table1">
<tr>
<td id="td1">
<a href="#" target="_blank" onclick="changeColor()">google</a>
</td>
<td id="td2">
<a href="#" target="_blank" onclick="changeColor()">baidu</a>
</td>
<td id="td3">
<a href="#" target="_blank" onclick="changeColor()">douban</a>
</td>
<td id="td4">
<a href="#" target="_blank" onclick="changeColor()">renren</a>
</td>
</tr>
</table></body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Demo_For_Wang</title><script type="text/javascript">
function changeColor(){
//取获取table的对象
var t=document.getElementById("table1").childNodes.item(0);
//遍历table每一个单元格,将其背景色清空(事实上是设置为白色)
for(var i=0;i< t.childNodes.length;i++)
{
for(var j=0;j<t.childNodes(i).childNodes.length;j++)
{
t.childNodes(i).childNodes(j).bgColor="white";
}
}
//获取点击的超链接所在的单元格对象
var cell = event.srcElement.parentElement;
//设置背景为红色
cell.bgColor = "red";
}</script></head><body>
<table border="1px" id="table1">
<tr>
<td id="td1">
<a href="javascript:changeColor('google')" target="_blank">google</a>
</td>
<td id="td2">
<a href="javascript:changeColor('baidu')" target="_blank" >baidu</a>
</td>
<td id="td3">
<a href="javascript:changeColor('douban')" target="_blank" >douban</a>
</td>
<td id="td4">
<a href="javascript:changeColor('renren')" target="_blank" >renren</a>
</td>
</tr>
</table></body>
</html>为什么下面的程序不合适啊? <a href="#" onclick="js()"> 与 <a href="javascript:js()">的区别是什么啊?
就是相当于你在浏览器的地址栏里输入
javascript:js()<a href="#" onclick="js()">
这个就是点击才执行js()函数了
这种指定的协议类型说明了URL的内容是js解释器将要运行的js代码的一个任意的字符串.它被当做单独一行代码对待.
例如:
javascript: var now =new Date();"<h1>Time is:</h1>"+now;
}
把你的参数也加上 。
和onclick能起到同样的效果,一般来说,如果要调用脚本还是在onclick事件里面写代码,而不推荐在href='javascript:function()' 这样的写法,因为 href 属性里面设置了js代码后,在某些浏览器下可能会引发其他不必要的事件。造成非预期效果。而且 onclick事件会比 href属性先执行,所以会先触发 onclick 然后触发href,所以如果不想页面跳转,可以设置 onclick里面的js代码执行到最后返回一个false,这样 href 里面的东西就不会执行了。在ajax应用程序中,多写着下面的这样 ,以表示这个链接不跳转,而执行一段js脚本。
<a href="javascript:void(0);" onclick="function()"></a>
或者 <a href="javascript:;" onclick="function()"></a>void(0) 只是用来计算一个空值,其实也是什么事情都不做,
而分号“;”则表示是一个空的js语句,这样就不会有任何其他跳转发生了,而且W3C标准不推荐在href里面执行javascript语句,所以还是用 onclick事件触发吧
根本就不是onclick了,所以根本没有事件参数 event
你可以 alert(event)看看结果是什么
所以自然就不能通过event获得对象了,也就不能设置背景色了