簡體   English   中英

jQuery代碼刪除具有相同內容的所有跨度但保留一個

[英]jQuery code to delete all spans with same content but keep one

說我有以下html:

<span class="fruit">Apple</span>
<span class="fruit">banana</span>
<span class="fruit">Apple</span>
<span class="fruit">Apple</span>
<span class="fruit">orange</span>

我嘗試了不同的方法,但它不起作用,我想要一個jQuery代碼刪除所有(.fruit)跨度相同的內容,但保留一個(如果可能的話,第一個),所以我將最終得到以下內容:

<span class="fruit">Apple</span>
<span class="fruit">banana</span>
<span class="fruit">orange</span>

謝謝

 $("span.fruit:contains(Apple):not(:first)").remove();
$('span.fruit').each(function(){
  return $('span.fruit:contains('+$(this).text()+'):not(:first)').remove();
})
var temp = array[];

$(".fruit").each(function(i) {
    var html = $($this).html();
    if($.inArray(html, temp)) {
        $($this).remove();
    }
    else {
        temp.push(html);
    }

});
temp = $('span:first').clone(true);
//remove all the span
$().html();
$().append(temp);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM