繁体   English   中英

如何替换上的文字 <td> 在一个 <table> -使用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_dateskey / 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM