jQuery更改链接中的文本

时间:2011-09-30 13:13:20

标签: javascript jquery

我对jQuery有一个简单的问题......

我有一个包含这样的链接的表

<table>
  <tr>
    <td class="views-field">
      <a href="ciao">201105</a>
    </td>
  </tr>
</table>

现在我将链接的文本从201105更改为2011-05

(在前4个字符后添加“ - ”)

我尝试了子串但不起作用......帮助我!!

3 个答案:

答案 0 :(得分:5)

这将翻译所有td.views-field链接:

$('td.views-field a').each(function () {
  var oldText = $(this).text();
  $(this).text(oldText.substr(0,4) + '-' + oldText.substr(4));
});

答案 1 :(得分:0)

尝试下面的代码,这应该可以正常工作。

var replaceElement = $("td.views-field a").first();
var oldDate = replaceElement.text();

var newDate = oldDate.substring(0, 4) + "-" + oldDate.substring(4,6);
replaceElement.text(newDate);

答案 2 :(得分:0)

$("a").text($("a").text().substring(0,4)+"-"+$("a").text().substring(4,6) );