【问题标题】:How to replace the text on <td> in a <table> - using javascript?如何在 <table> 中替换 <td> 上的文本 - 使用 javascript?
【发布时间】:2014-08-29 07:10:49
【问题描述】:

我有一个 &lt;table&gt; 和一个 &lt;td&gt;,我想在其中替换文本 - 使用 javascript。

<table class="table">
  <tbody>
    <tr>
      <td id="53ffaf3e436872c452020000">
        2014-08-16T11:00:00.000+02:00
      </td>
    </tr>
  </tbody>
</table>

我从服务器收到一个 key/value 循环的 new_dates 对象:

咖啡脚本版本:

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's 中找到所有id's,并将它们的文本值替换为value's。但是,我的代码不起作用。我做错了什么?

【问题讨论】:

    标签: javascript jquery html coffeescript


    【解决方案1】:

    jQuery.text 是一个函数。您正在尝试将其用作属性。试试这个:

    $("#" + key).text(last_date[key]);
    

    您不需要在前面添加一个空字符串 - 作为文本节点插入的任何内容都将转换为字符串。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-12
      • 1970-01-01
      • 1970-01-01
      • 2015-06-22
      • 1970-01-01
      • 2013-10-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多