简体   繁体   English

jQuery:如何更改网格单元格中特定div的内容?

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

Currently i am using this to modify the "html" of a specific "column/cell" by finding the "td" of the "grid" at specific "rows". 当前,我正在使用它通过在特定“行”处找到“网格”的“ td”来修改特定“列/单元格”的“ html”。

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

After that is done, how do I [ (a) access, (b) modify ] the individual "div1/div2" content that is in it for the "Grid" at particular "row" and "column"? 完成之后,我如何[(a)访问,(b)修改]特定“行”和“列”中“网格”的单个“ div1 / div2”内容? RowNumber = 2, column = 26 RowNumber = 2,列= 26

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

Thanks to all for the answer. 感谢所有的答案。 I like best is this with just a single line. 我最喜欢的只是一行。

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

Try this 尝试这个

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

Do not mess with the 4. row 26. column. 不要弄乱4.行26.列。 This is a bad development practice. 这是一个不良的开发实践。 Instead you should give an id or class to exact column then select it with jQuery and do what you want to do. 相反,您应该为确切的列提供一个ID或类,然后使用jQuery选择它或做您想做的事情。 such as

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

When you have your element at the specified index: 当您的元素位于指定的索引处时:

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

You can use this to access something inside. 您可以使用this来访问内部内容。 Like so: 像这样:

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

Or even make your own selector a bit shorter: 甚至使自己的选择器更短:

$("#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