最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - jQuery change text in link - Stack Overflow

matteradmin6PV0评论

I have a simple question for jQuery...

I have a table with a link like this

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

Now I would change the link's text from 201105 to 2011-05

(simple add a "-" after the first 4 characters)

I tried substring but don't work... Help me!!

I have a simple question for jQuery...

I have a table with a link like this

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

Now I would change the link's text from 201105 to 2011-05

(simple add a "-" after the first 4 characters)

I tried substring but don't work... Help me!!

Share Improve this question edited Sep 30, 2011 at 13:35 Matt 75.3k26 gold badges156 silver badges180 bronze badges asked Sep 30, 2011 at 13:13 Fra OreFra Ore 151 silver badge5 bronze badges 2
  • Can you post the code you tried? It would make pointing out the error easier. – Frédéric Hamidi Commented Sep 30, 2011 at 13:14
  • What have you tried? We're happy to help but would rather not do your homework :) – Kerry Jones Commented Sep 30, 2011 at 13:14
Add a ment  | 

3 Answers 3

Reset to default 5

This will translate all td.views-field links:

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

Try the code below, that should work fine.

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

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

comment list (0)

  1. No comments so far