簡體   English   中英

如何通過ID jQuery從tr訪問td

[英]How to access td from tr with id Jquery

我的tr有id和兩欄(tds),我想通過這樣做來動態地更改第二個td html:

$("#value_"+ control_id+":nth-child(2)").html(type_id_name);

但它會擦除所有tds,並在on列中插入html。

這是表結構

<tr id="value_486">
  <td></td>
  <td></td>
<tr>

我嘗試了不同的方法

$("#tableID tr#value_"+ control_id+":nth-child(2)").html(type_id_name);

但它也不起作用。

您需要在:nth-child之前加空格或td 空格表示子選擇器或子代選擇器:

$("#tableID tr#value_"+ control_id+" :nth-child(2)").html(type_id_name);
$("#tableID tr#value_"+ control_id+" td:nth-child(2)").html(type_id_name);

您當前的那個沒有空格:

$("#tableID tr#value_"+ control_id+":nth-child(2)").html(type_id_name);

選擇<tr>的第二個實例,該實例不存在!

 $(function () { var control_id = 486; var type_id_name = "Praveen"; $("#tableID tr#value_"+ control_id+" :nth-child(2)").html(type_id_name); $("#tableID tr#value_"+ control_id+" td:nth-child(2)").html(type_id_name); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="tableID"> <tr id="value_486"> <td></td> <td></td> <tr> </table> 

是的,當您使用#id進行選擇時,不需要兩個#id選擇器。 您的第一種風格有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM