首页 > 解决方案 > 如何在 JavaScript 中获取 td id

问题描述

<table class="table" style="margin-bottom:0px!important"> 
    <b>
    <? if(!empty($channelBase['rep_ids']))
    {
        $s_id=$channelBase['id'];
        $i=0;   
        $temp='';

        $reps_channl=explode(",",$channelBase['rep_ids']);
        foreach($reps_channl as $k)
        {
            $added_reps = $rep_names[$k];
            if($i==0){
            $temp.='<tr style="border-top:none;">';     
            }
            $temp.="<td id='".$s_id."_".$k."' class='repclicked' style='border-top:none;'>$added_reps <a href='#' class='btn btn-xs btn-icon btn-circle ' onclick='delete_repid($k,$s_id);'><i class='fa fa-close'></i></a>
            </td>";
            $i++;
            if($i==5)
            {
            $temp.='</tr>';
            $i=0;
            }
        }
        echo $temp;
    }
    ?>
    </b>
</table>

在这里,我有一个td带有数字值的 id。在这里,我对如何td在 javascript 中获取 id 感到困惑。谁能帮帮我吗。

标签: javascriptjqueryajaxcodeigniter

解决方案


这是您如何进入Idstd表的方法

for (let row of mytab1.rows) 
{
    for(let cell of row.cells) 
    {
       console.log(cell.id)
    }
}
<div id="myTabDiv">
<table name="mytab" id="mytab1">
  <tr> 
    <td id="id_1">col1 Val1</td>
    <td id="id_2">col2 Val2</td>
  </tr>
  <tr>
    <td id="id_3">col1 Val3</td>
    <td id="id_4">col2 Val4</td>
  </tr>
</table>
</div>


推荐阅读