2017-09-22 62 views
1

是否可以比较2个div的内容以及是否有任何内容匹配,将它从第二个实例中删除?jQuery比较Div内容并删除任何重复的数据

在下面的例子中,我们将看到“2018春季会议”已经出现在h2中,因此我们会将它定位并从下面的列表项中移除它。

理想情况下,我们会留下“会员注册”,“非会员注册”和“访客注册”。

<h2>2018 Spring Conference and Registration</h2> 
<ul class="prod-options-list"> 
<li> 
    <h4><a href="#">2018 Spring Conference Member Registration</a></h4> 
</li> 
<li> 
    <h4><a href="#">2018 Spring Conference Non-Member Registration</a></h4> 
</li> 
<li> 
    <h4><a href="#">2018 Spring Conference Guest Registration</a></h4> 
</li> 
</ul> 
+0

下做督促选项列表每个H4的。每次,比较以前的,如果是相同的删除它,继续前进 – clearshot66

+0

'li's中的预期结果是“Member”,“Non-Member”和“Guest”? –

+0

关闭@CommercialSuicide。我正在寻找“会员注册”,“非会员注册”和“访客注册”。对不起,我应该指定。将编辑。 – Greg

回答

3

在这里你去,使用jQuery(fiddle

var h2text = $('h2').text().split(' '); 

$('a').each(function() { 
    var t = $(this).text(); 

    h2text.forEach(function (e) { 
     t = t.replace(e, ""); 
    }); 

    $(this).text(t); // this removes any match of repeated words 

    // if you need to put "Registration", then use this line instead of the above: 
    $(this).text(t + 'Registration'); 


}); 
相关问题