[英]Replace text in an element in HTML from another element (table) using jQuery
I want to replace a particular text in an HTML with another text from another element, which in this case is a table data.(). 我想用另一个元素中的另一个文本替换HTML中的特定文本,在本例中是一个表格数据。()。
Here is my table: 这是我的表:
<table id="hftable">
<tr>
<td id="hfpn1">V.Sehwag</td>
<td id="hfp1">DNB</td>
<td id="hfp1b">.-.-..-.</td>
</tr>
<tr>
<td id="hfpn2">G.Gambhir</td>
<td id="hfp2">DNB</td>
<td id="hfp2b">.-.-..-.</td>
</tr>
<tr>
<td id="hfpn3">A.Arora</td>
<td id="hfp3">DNB</td>
<td id="hfp3b">.-.-..-.</td>
</tr>
</table>
where batsman1 is the id of the element where the replacement is to take place or more specifically the table looks like: 其中batsman1是要进行替换的元素的id,或者更具体地说,表格如下所示:
<table id="current">
<tr>
<td id="batsman1">Batsman 1</td>
<td id="currentbat1"></td>
</tr>
<tr>
<td id="batsman2">Batsman 2</td>
<td id="currentbat2"></td>
</tr>
<tr>
<td>Bowler</td>
<td></td>
</tr>
</table>
I want to replace Batsman 1 (in the table with id="current") with V.Sehwag (in the table with id="hftable") (when someone clicks on a specific button on the page) 我想用V.Sehwag替换Batsman 1(在id =“current”的表中)(在id =“hftable”的表中)(当有人点击页面上的特定按钮时)
I have tried: 我努力了:
$("#batsman1").text($("#hfpn1").val());
This isn't working. 这不起作用。 Apart of showing "V.Sehwag" in place of "Batsman 1", it is showing me a blank space. 除了显示“V.Sehwag”代替“Batsman 1”之外,它还向我展示了一个空白区域。
Any ideas where I am wrong? 我错的任何想法? What is the correct and easy way to do this? 这样做的正确和简单方法是什么? Thank You. 谢谢。
The TD element does not have a value, it has text. TD元素没有值,它有文本。 Modify your code as follows: 修改您的代码如下:
$("#batsman1").text($("#hfpn1").text());
Try to use .text()
in this context, You are invoking .val()
function over a non-input element, obviously it wont work. 尝试在此上下文中使用.text()
,您正在非输入元素上调用.val()
函数,显然它不会工作。 And do remember that, .val()
only works on input elements on which user can input something via the UI. 并且要记住, .val()
仅适用于用户可以通过UI输入内容的输入元素。
$("#batsman1").text($("#hfpn1").text());
使用下面的内容,我希望这可以帮助您解决问题。
$("#batsman1").html($("#hfpn1").text());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.