日期:2014-05-16 浏览次数:20368 次
<!DOCTYPE HTML> <html> <head> <meta charset="gb2312" /> <title></title> <style> </style> </head> <body> <table> <tr> <td>1<td> <td>2<td> <tr> <tr> <td>3<td> <td>4<td> <tr> <table> <script> var $ = function(id){ return document.getElementById(id); }; var $t = function(tag, cot){ cot = cot || document; return cot.getElementsByTagName(tag); }; var objs = $t('td'); for(var i = 0, len = objs.length; i < len; i++){ objs[i].onclick = function(){ alert( this.innerHTML ) } } </script> </body> </html>
------解决方案--------------------
<table> <tr> <td onclick="">1<td> <td>2<td> <tr> <tr> <td>3<td> <td>4<td> <tr> <table> <script type="text/javascript"> var tds=document.getElementsByTagName("td"); var obj=null; for(var i=0,len=tds.length;i<len;i++){ obj=tds[i]; obj.onclick=function(){ alert(this.innerHTML); } } </script>