使用jquery从段落中删除某些单词

时间:2012-01-23 19:19:20

标签: jquery html

我有一个这样的网页:

<p class="author">By ... on Jan 23, 2012</p>
<p class="content">(some content)</p>

<p class="author">By ... on Jan 23, 2012</p>
<p class="content">(some content)</p>

<p class="author">By ... on Jan 23, 2012</p>
<p class="content">(some content)</p>

...

我想使用jquery从p.author中删除“By”和“on”,结果将是:

<p class="author">... Jan 23, 2012</p>
<p class="content">(some content)</p>
...

谢谢!

3 个答案:

答案 0 :(得分:8)

$('.author').each(function(){
$(this).text($(this).text().replace(/on|by/g,""))
});

答案 1 :(得分:2)

无需额外each

$("p.author").text(function() {
    return $(this).text().replace(/(By|on)/g, '');
});

答案 2 :(得分:2)

$(".author").each( function(){
    var text = this.firstChild.nodeValue;
    this.firstChild.nodeValue = text.replace( /(^By|\bon\b)/g, "" );
});