jquery - 使用 jquery 删除重复文本

标签 jquery

假设我有以下内容

 <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 feed 动态提取的。

提前致谢

最佳答案

如果您的 DOM 结构仍然如上面所述,那么以下内容将适合您:

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

<强> DEMO

根据您的评论

<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(), '');
});

<强> DEMO

关于jquery - 使用 jquery 删除重复文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10991459/

相关文章:

javascript - jQuery 删除附加元素

jquery - $ ("html").mask 在 IE7 中不起作用

jquery - 使用 jQuery Ajax 调用下载并保存文件

javascript - 如何修改表单操作添加选定的单选按钮值

javascript - Ajax 代码表单在提交时不起作用

jquery - 滚动时移动图像

jquery - 将元素覆盖在 CSS 中的副本上

jquery - 通过服务器端处理隐藏 DataTable JSON 中存在的列

javascript - 如何检索 jquery 插件附加的目标元素?

javascript - 所选属性未在我的选择对象中设置