jquery remove text partially

前端 未结 3 1220
逝去的感伤
逝去的感伤 2020-12-21 06:09

Can i use jQuery to remove part of a text within a div.

Like so:

Published May 18th 2011 - Approuved - Expire May 18 th 2
相关标签:
3条回答
  • 2020-12-21 06:50

    Try this:

    $(".entry").html(function(i, htm){
       return htm.split("-")[0];
    });
    

    html() can also have a function passed on to it if you want to process the html inside a node and then reset it.

    Here is a demo on jsfiddle : http://jsfiddle.net/nKJWn/

    0 讨论(0)
  • 2020-12-21 06:54
    $(".entry").html(($(".entry").html().replace("Approuved - Expire May 18 th 2012","")))
    
    0 讨论(0)
  • 2020-12-21 06:55

    You can use jquery to select your element/html, but javascript has a builtin function replace that will do what you need:

    $('div.entry').html($('div.entry')
                              .html()
                              .replace('Approuved - Expire May 18 th 2012', ''))
    

    Or using RegExp:

    If what you want to replace starts always with Approuved - Expire:

    $('div.entry').html($('div.entry')
                              .html()
                              .replace(/Approuved - Expire[^<]*/, ''))
    
    0 讨论(0)
提交回复
热议问题