簡體   English   中英

jQuery:如何更改網格單元格中特定div的內容?

[英]Jquery: How do i change the content of a specific div in a cell of a grid?

當前,我正在使用它通過在特定“行”處找到“網格”的“ td”來修改特定“列/單元格”的“ html”。

$("#VGrid1_MainGrid tr:nth-child(" + iRowNumberGrid + ")").find('td').eq(26).html("HTMLHERE");

完成之后,我如何[(a)訪問,(b)修改]特定“行”和“列”中“網格”的單個“ div1 / div2”內容? RowNumber = 2,列= 26

<td>
<div class="myclass">
      <div id="div1">Access content</div>
      <div id="div2">Add/Modify content</div>
</div>
<td>

感謝所有的答案。 我最喜歡的只是一行。

$("#VGrid1_MainGrid tr:nth-child(" + iRowNumberGrid + ")").find('td').eq(26).find('#div1').html('stuffhere');

嘗試這個

var td = $("#VGrid1_MainGrid tr:nth-child(" + iRowNumberGrid + ")").find('td').eq(26);
td.html("HTMLHERE");
$("#div1", td).html("foo");
$("#div2", td).html("bar");

不要弄亂4.行26.列。 這是一個不良的開發實踐。 相反,您應該為確切的列提供一個ID或類,然后使用jQuery選擇它或做您想做的事情。

var col = $('#row4column26'); // this will select you 26th column at 4th row 
col.find('#div1').html('HTMLHERE'); // select the div1 in the column and change its content
col.find(('#div2').html('HTMLHERE'); // the same for div2

當您的元素位於指定的索引處時:

$("#VGrid1_MainGrid tr:nth-child(" + iRowNumberGrid + ")").find('td').eq(26)

您可以使用this來訪問內部內容。 像這樣:

$("#VGrid1_MainGrid tr:nth-child(" + iRowNumberGrid + ")").find('td').eq(26).each(function()
{
    $(this).html("HTMLHERE");
    $('#div1',this).doSomething();
});

甚至使自己的選擇器更短:

$("#VGrid1_MainGrid tr:nth-child(" + iRowNumberGrid + ") td").eq(26).each(function()
{
    $(this).html("HTMLHERE");
    $(this).find('#div1').doSomething();
}

當您已經找到div所在的列時(上面的代碼),您可以在列的jQuery對象上使用.find('#div1')來檢索div並使用.html('stuffhere')進行修改

暫無
暫無

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

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