问一个点击问题
怎样实现这样的点击效果:
以下是百度网页搜索代码:
- HTML code
<table> <tr align="center"> <td bgcolor="#00FFFF"><a href="javascript:">网页</a></td> <td><a href="javascript:">音乐</a></td> </tr> <tr> <td colspan="2"> <form action="http://www.baidu.com/s" target="_blank"> <input name="word" type="text" value="" /> <input type="submit" value="百度一下" /> </form></td> </tr></table>
怎样实现点击“音乐”链接后,单元格改变颜色,搜索代码改为音乐搜索?效果:
- HTML code
<table> <tr align="center"> <td><a href="javascript:">网页</a></td> <td bgcolor="#00FFFF"><a href="javascript:">音乐</a></td> </tr> <tr> <td colspan="2"> <form action="http://mp3.baidu.com/m" target="_blank"> <input name="word" type="text" value=""> <input type="submit" value="百度一下"></form></td> </tr></table>
[解决办法]
- HTML code
<!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>Untitled Document</title><script type="text/javascript">window.onload = function() { var tr1 = document.getElementsByTagName('table')[0].getElementsByTagName('tr')[0]; var f = document.getElementsByTagName('table')[0].getElementsByTagName('form')[0]; tr1.getElementsByTagName('td')[0].onclick = function() { this.style.backgroundColor = '#0FF'; tr1.getElementsByTagName('td')[1].style.backgroundColor = '#FFF'; f.action = 'http://www.baidu.com/s'; } tr1.getElementsByTagName('td')[1].onclick = function() { this.style.backgroundColor = '#0FF'; tr1.getElementsByTagName('td')[0].style.backgroundColor = '#FFF'; f.action = 'http://mp3.baidu.com/m'; }}</script></head><body><table> <tr align="center"> <td style="background-color:#0FF"><a href="javascript:">网页</a></td> <td><a href="javascript:">音乐</a></td> </tr> <tr> <td colspan="2"> <form action="http://www.baidu.com/s" target="_blank"> <input name="word" type="text" value="" /> <input type="submit" value="百度一下" /> </form></td> </tr></table></body></html>
[解决办法]
- HTML code
<script>function doit(n){ var tbl = document.body.getElementsByTagName("table")[0]; for(var i=0;i<tbl.rows[0].cells.length;i++) tbl.rows[0].cells[i].style.background = "#FFF"; switch(n){ case 0: tbl.rows[0].cells[0].style.background = "#0FF"; document.forms[0].action = "http://www.baidu.com/s"; break; case 1: tbl.rows[0].cells[1].style.background = "#0FF"; document.forms[0].action = "http://mp3.baidu.com/m"; break; }}</script><table> <tr align="center"> <td bgcolor="#00FFFF"><a href="javascript:doit(0);void(0);">网页</a></td> <td><a href="javascript:doit(1);void(0);">音乐</a></td> </tr> <tr> <td colspan="2"> <form action="http://www.baidu.com/s" target="_blank"> <input name="word" type="text" value="" /> <input type="submit" value="百度一下" /> </form></td> </tr></table>