скрыть дубликат div в jquery - PullRequest
       4

скрыть дубликат div в jquery

1 голос
/ 07 сентября 2010

У меня есть несколько слоев, которые динамически вставляются следующим образом

<div><p class="locid">2<p></div>
<div><p class="locid">1<p></div>
<div><p class="locid">2<p></div>
<div><p class="locid">3<p></div>
<div><p class="locid">4<p></div>

Что мне нужно сделать, это скрыть второе вхождение этого слоя, чтобы он выглядел следующим образом

<div><p class="locid">2<p></div>
<div><p class="locid">1<p></div>
<div><p class="locid">3<p></div>
<div><p class="locid">4<p></div>

Есть идеи?

Спасибо

Джейми

Ответы [ 5 ]

1 голос
/ 07 сентября 2010

Интересно. Попробуйте это.

var a = new Array();
$('p.locid').each(function(){
    text = $(this).text();
    if($.inArray(text, a)){
        $(this).closest('div').hide();
    }else{
        a.push(text);
    }
});
1 голос
/ 07 сентября 2010

Посмотрите на:

http://api.jquery.com/jQuery.unique/

Это именно то, что вы ищете;)

1 голос
/ 07 сентября 2010
// get a collection of p's matching some value
$("p.locid").filter(function() {
    return $(this).text() == '2';

// hide the (div) parent of the second match
}).eq(1).parent().hide();

Демо: http://jsfiddle.net/WjgxQ/

0 голосов
/ 16 мая 2012

Это работает:

var a = new Array();

$('p').each(function(index) {
    text = $(this).text();
    if($.inArray(text, a)!=-1){
        $(this).closest('p').hide();
    }else{
        a.push(text);
    }
});

http://jsfiddle.net/WjgxQ/59/

0 голосов
/ 07 сентября 2010

Попробуйте это:

var arr = new array();

$('.locid').each(function(){
  if ($.inArray($(this).text(), arr) !== -1){
     $(this).closest('div').remove();
  }
  else{
    arr[] = $(this).text();
  }
});
...