繁体   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