2

说我有以下

 <h3>This is my heading</h3>
 <p>This is my headingAnd this is part of a paragraph</p>

我想结束以下

 <h3>This is my heading</h3>
 <p>And this is part of a paragraph...</p>

我找到了这段代码

   $(function() {
   var seen = {};
    $('p, h3').each(function() {
    var txt = $(this).text();
     if (seen[txt])
     $(this).remove();
   else
     seen[txt] = true;
  });
});

但是因为段落中加入了两个单词,所以它不起作用。数据是从 rss 提要中动态提取的。

提前致谢

4

2 回答 2

3

如果您的 DOM 结构保持如上所述,那么以下内容将为您工作:

$('p').text(function(index, oldText) {
  return oldText.replace($(this).prev('h1').text(), '');
});

演示

根据你的评论

<div id="youtube">
    <section>
        <h3>Launch of the Active Living impact checklist</h3>
        <p>Launch of the Active Living impact checklistFrom test</p>
        <a target="_blank" href="#"> watch video</a> <br>
    </section>
    <section>
        <h3>Enterprise Computing Conference</h3>
        <p>Enterprise Computing ConferenceWorld leaders in enterprise computing attended a two</p>
        <a target="_blank" href="#"> watch video</a> <br>
    </section>
</div>

$('p').text(function(index, oldText) {
  return oldText.replace($(this).prev('h3').text(), '');
});

演示

于 2012-06-12T06:29:05.303 回答
0
var header = ​$('h1').​​​​text();
var body = $('p').text();

if (body.indexOf(header) >= 0) {
    $('p').text(body.replace(header, ''));
}

演示

于 2012-06-12T06:31:51.327 回答