如何在特定字符之前或之后删除网页上的文本

时间:2018-05-29 08:50:41

标签: jquery css

我在 WordPress 网站上有一个标题列表,后跟源名称,日期和摘录。我想保留标题,来源归属和日期,但想要在“我是”之后摆脱所有事情。或者' pm'在日期(摘录)。有没有办法做到这一点?

示例:

NBA总决赛:勇士队将再次面对骑士队

今日美国 - NBA | 2018年5月29日上午5:39体育脉搏:今日美国体育'Sam Amick打破了勇士队第7场战胜火箭队的比赛,以及他们将如何连续第四场比赛。

包含内容的HTML:



<div class="entry-content cf" itemprop="text">
  <div class="entry-excerpt">
    <p>
    Boston Herald &#8211; Boston Celtics | May 29, 2018 8:47 am Here we go again. And again. And, well, again. Round 4 &mdash; as many predicted, and others probably lamented &mdash; is happening. Cleveland and Golden
    </p>
  </div>
</div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

假设您正在使用jQuery。 https://jsfiddle.net/andreitodorut/t4fvqoyw/

$(document).ready(function() {

    $('.title').each(function() {
    $(this).text(
        $(this).text().substr(0,
          $(this).text().indexOf(" am") > -1 ?
             $(this).text().indexOf(" am") + 3 :
             $(this).text().indexOf(" pm") + 3)
    )
  })

});

答案 1 :(得分:0)

另一个javascript / jQuery选项(可能有很多更好的方法)..

&#13;
&#13;
$('.entry-excerpt').each(function() {
  var string = $(this).find('p').text();
  if ($(this).find('p').is(':contains(" am ")')) {
    amString = string.substring(0, string.indexOf(' am '));
    $(this).find('p').text(amString + ' am');
  } else if ($(this).find('p').is(':contains(" pm ")')) {
    pmString = string.substring(0, string.indexOf(' pm '));
    $(this).find('p').text(pmString + ' pm');
  }
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="entry-excerpt">
  <p>
  NBA Finals: Warriors will face Cavaliers yet again

  USA Today – NBA | May 29, 2018 5:39 am SportsPulse: USA TODAY Sports’ Sam Amick breaks down the Warriors’ Game 7 win over the Rockets, and how they’ll match up in a fourth conseuctive
  </p>
</div>
<div class="entry-excerpt">
  <p>
  Boston Herald &#8211; Boston Celtics | May 29, 2018 8:47 pm Here we go again. And again. And, well, again. Round 4 &mdash; as many predicted, and others probably lamented &mdash; is happening. Cleveland and Golden
  </p>
</div>
&#13;
&#13;
&#13;

<强>拨弄

https://jsfiddle.net/Hastig/o8hedrsk/

<强>信用

https://stackoverflow.com/a/5631434/3377049