![](/img/trans.png)
[英]How to get text of link that inside td in HTML Table using JavaScript?
[英]How to replace the text on <td> in a <table> - using javascript?
我有一個<table>
和一個<td>
,我想在其中替換文本-使用javascript。
<table class="table">
<tbody>
<tr>
<td id="53ffaf3e436872c452020000">
2014-08-16T11:00:00.000+02:00
</td>
</tr>
</tbody>
</table>
我從服務器收到一個new_dates
的key
/ value
對的new_dates
對象:
Coffeescript版本:
last_dates.map (last_date) ->
for key of last_date
console.log key + " has the date: " + last_date[key] # 53ffb262436872c499b90f00 has the date: 2014-08-16T11:00:55.000+02:00
$("##{key}").text = "#{last_date[key]}"
JavaScript版本:
last_dates.map(function(last_date) {
var key, _results;
_results = [];
for (key in last_date) {
console.log(key + " has the date: " + last_date[key]); // 53ffb262436872c499b90f00 has the date: 2014-08-16T11:00:55.000+02:00
_results.push($("#" + key).text = "" + last_date[key]);
}
return _results;
});
上面的代碼應該從key
找到所有id,並將其文本值替換為value
。 但是,我的代碼不起作用。 我究竟做錯了什么?
jQuery.text
是一個函數。 您正在嘗試將其用作屬性。 嘗試以下方法:
$("#" + key).text(last_date[key]);
您不需要在前面加上一個空字符串-您插入為文本節點的任何內容都將轉換為字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.