开发者

Remove part of text within P tag after a " " separator

My Markup is like this :

<div class="article">
 <p class="date">16-05-2011 15:28:24</p>
</div>

This output is generated by a CMS and I searched for everywhere to modify the output but I didn't reach a solution.

I want to use jQuery to remove the "hour" from the date paragraph.

Saying I don't want any text after开发者_JAVA百科 the "space" so that the day-month-year will show up only.

Any idea how to achieve this using jQuery?


Pretty easy loop, splitting by a space with a limit of 1 item:

$('p.date').each(function() {
    $(this).html($(this).html().split(" ",1)[0]);
});


<script type="text/javascript">
  var date = $('p.date').html(); // get date content
  var split = date.split(" "); // explode by space
  $('div.date').html(split[0]); // setting the html with the first piece
</script>

not tested, but I think this should be it. If there are more then 1 div, try this:

<script type="text/javascript">
  $(document).ready(function() {

    $('p.date').each(function(index) {
      var date = $(this).html();
      var split = date.split(' ');
      $(this).html(split[0]);
    });

  });
</script>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜