Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从另一个单元格更改单元格的内容_Javascript_Jquery_Html - Fatal编程技术网

Javascript 从另一个单元格更改单元格的内容

Javascript 从另一个单元格更改单元格的内容,javascript,jquery,html,Javascript,Jquery,Html,哎,, 我正在尝试使用jquery选择“更改单元格内容”,但似乎无法使其正常工作。假设我们有这个表: <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(

哎,, 我正在尝试使用jquery选择“更改单元格内容”,但似乎无法使其正常工作。假设我们有这个表:

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

第1单元
第2单元
第三单元
第4单元
第5单元
单元格6单击
第7单元
第8单元
第9单元
我想从第6单元更改第4单元的内容。“td”显然不起作用。如何在不使用类或ID的情况下从单元6访问单元4?
提前感谢

你会想要这个,因为你想上一个,后两个

$(this).parent().prev().prev().html('test');
或者像这样:

$(this).parent().prevAll(':last').html('test');
或者这个:

$(this).parent().siblings(':first-child').html('test');
…或者不使用jQuery也可以轻松完成:

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

将javascript更改为:

$(this).parent().parent().子类('td').eq(0).html('test')


这将获取表行(单元格4)的第一个子td,并设置HTML。

如果要更改表中的第四个
,可以转到最近的
表,然后
在其中查找第四个
。然后您可以更改它的HTML

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