2017-09-22 63 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'); 


}); 
相關問題