开发者

Change the content of a cell from another cell

hey, i'm trying to select change the content of a cell with jquery but i cant seem to get it working. lets say we have this table:

<table border="1">
<tr>
<td>cell 1
<td>cell 2
<td>cell 3
<tr>
<td>cell 4
<td>cell 5
<td>cell 6 <a onclick="javascript:$(this).parent().parent().('td').html('test');">Click</a>
<tr>
<td>cell 7
<td>cell 8
<td>cell 9
</table>

i want to change the content of cell 4 from within cell 6. the ('td') obviously isnt working. how can i access cell 4 from cell 6 without the use of classes or ids?开发者_如何学JAVA thanks in advance


You'll want this instead since you want to go up one, and back two.

$(this).parent().prev().prev().html('test');

or like this:

$(this).parent().prevAll(':last').html('test');

or this:

$(this).parent().siblings(':first-child').html('test');

...or you could do it pretty easily without jQuery:

this.parentNode.parentNode.cells[0].innerHTML = 'test';


Change the javascript to be:

$(this).parent().parent().children('td').eq(0).html('test');

This gets the first child td of the table row (cell 4), and sets the HTML.


If you want to change the 4th <td> in your table, you can go up to the closest table, and then find the 4th <td> inside it. Then you can change its HTML.

$(this).closest('table').find('td:eq(3)').html('test');

Live Example

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜