日期:2014-05-16  浏览次数:20368 次

高手求救,用js 在table中点击(onclick)td时怎样才能获取td的值?
<table>
  <tr>
  <td onclick="">1<td>
  <td>2<td>
  <tr>
  <tr>
  <td>3<td>
  <td>4<td>
  <tr>
<table>

点击哪个td就获取那个td的值,如何实现呢?

------解决方案--------------------
HTML code



<!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>

------解决方案--------------------
HTML code
<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>