Javascript 使用rowIndex和cellIndex更改表格单元格中的文本
我得到了一行的索引。列计数是静态的,因此我知道在哪个列中放置什么,但我希望能够使用onclick事件处理程序更改特定单元格中的内容。应用了此onclick事件的元素位于表外,包含我要从中复制内容的所有文本输入。一个简单的例子Javascript 使用rowIndex和cellIndex更改表格单元格中的文本,javascript,jquery,html,Javascript,Jquery,Html,我得到了一行的索引。列计数是静态的,因此我知道在哪个列中放置什么,但我希望能够使用onclick事件处理程序更改特定单元格中的内容。应用了此onclick事件的元素位于表外,包含我要从中复制内容的所有文本输入。一个简单的例子 <table id="table1"> <tr> <td>SomeText</td> <td>SomeOtherText</td> </tr> <tr> <td>So
<table id="table1">
<tr>
<td>SomeText</td>
<td>SomeOtherText</td>
</tr>
<tr>
<td>SomeText2</td>
<td>SomeOtherText2</td>
</tr>
</table>
<div id="box1">
<form>
<input type="text" name="newText"/>
<input type="button" onclick="?" />
</form>
</div>
一些文字
其他文字
SomeText2
其他一些文本2
您可以使用以下方法访问此特定单元格:
$('#table1 tr:eq(1) td:eq(1)')
在这里,它将选择第二行的第二列(基于0的计数)
然后可以使用jQuery.text()
或jQuery.html()
您可以执行以下操作:
<table id="table1">
<tr>
<td>SomeText</td>
<td>SomeOtherText</td>
</tr>
<tr>
<td>SomeText2</td>
<td>SomeOtherText2</td>
</tr>
</table>
$('#table1 tr:eq(1) td:eq(1)').text("Whatever text you want");
或
您可以使用java脚本和id
<table id="table1">
<tr>
<td id=r1c1>SomeText</td>
<td id=r1c2>SomeOtherText</td>
</tr>
<tr>
<td id=r2c1>SomeText2</td>
<td id=r2c2>SomeOtherText2</td>
</tr>
</table>
<script>
function updatetxt(){
x=Document.getElementById(r1c1);
x.value=Document.getElementById(txt1).value
}
</script>
<div id="box1">
<form>
<input type="text" id=txt1 name="newText" />
<input type="button" onclick="updatetxt()" />
</form>
</div>
一些文字
其他文字
SomeText2
其他一些文本2
函数updateText(){
x=Document.getElementById(r1c1);
x、 value=Document.getElementById(txt1.value)
}
谢谢。希望它也适用于提出此问题的人。“tr:eq()”-我可以将javascript变量放入其中吗?我不确定它是否工作var yourVariable=1$(“#表1 tr:eq(“+yourVariable+”)td:eq(1)”).text(“您想要的任何文本”)
您只需要创建选择器字符串,这样您就可以像在javascript中处理任何其他字符串表达式一样拆分字符串并在两者之间插入。
<table id="table1">
<tr>
<td id=r1c1>SomeText</td>
<td id=r1c2>SomeOtherText</td>
</tr>
<tr>
<td id=r2c1>SomeText2</td>
<td id=r2c2>SomeOtherText2</td>
</tr>
</table>
<script>
function updatetxt(){
x=Document.getElementById(r1c1);
x.value=Document.getElementById(txt1).value
}
</script>
<div id="box1">
<form>
<input type="text" id=txt1 name="newText" />
<input type="button" onclick="updatetxt()" />
</form>
</div>