繁体   English   中英

jQuery更改链接中的文本

[英]jQuery change text in link

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

我有一张桌子,上面有这样的链接

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

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

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

我尝试过子字符串,但是不起作用...帮帮我!

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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