假设我有以下内容
<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/