开发者

how to remove duplicate div sections?

I am appending data output into this div section.

Within this <div> section, there are <div> sections that are duplicates. How can I remove the latter duplicates?

Example coding to remove:

<div id=EXTRACTION_a>something ...</div>
<div id=EXTRACTION_b>something ...</div>
<div id=EXTRACTION_c>something ...</d开发者_Python百科iv>
<div id=EXTRACTION_a>something ...</div>
<div id=EXTRACTION_b>something ...</div>
<div id=EXTRACTION_a>something ...</div>
<div id=EXTRACTION_c>something ...</div>
<div id=EXTRACTION_d>something ...</div>
<div id=EXTRACTION_a>something ...</div>

I want to keep the first occurances of a,b,c,d and remove the rest.

I tried this coding but not working:

            var count = -1;
            \$("div[id^='EXTRACTION_']").each(function(){
               count ++;
               var ids = \$('[id='+this.id+']');
               var originaldivsectin = \$(this);

               var count2 = 0;
               \$("div[id^='EXTRACTION_']").each(function(){
                    count2 ++;
                    var copyids = \$('[id='+this.id+']');
                    var dupdivsection = \$(this);
                    if (count != count2 && copyids.length>1 && ids[count]==copyids[count2])
                    {
                        dupdivsection.remove();   
                    }
               });

               // if(ids.length>1 && ids[0]==this)
               // {  
               //       \$(this).remove();
               //       alert('Multiple IDs #'+this.id);
               // }
            });

Note: jquery is still a new language for me


EDIT: Based on your update, I see that you only want to remove certain elements based on a potentially matching ID, and that there could be several matches of each type.

You can do this instead:

Click here to test a working example. (jsFiddle)

var found = {}; // Container to hold each unique ID that we find.

$('div[id^=EXTRACTION_]').filter(function() {
    var ending = this.id.replace("EXTRACTION_","");
    if( found.hasOwnProperty( ending ) ) {
        return this;
    } else {
        found[ ending ] = ending;
    }
}).remove();

Basically using the attribute-starts-with-selector(docs) we select all elements that start with EXTRACTION_. (I also added the div to the selector. If they're not all divs, remove that.)

Then we filter()(docs) them. In the filter, first we extract the unique part of the ID. Then we check to see if that ID is a property of an object we created called found. If it isn't, add it as a property. If it is already a property, that means we already came across it, so we do return this, which will cause the .filter() to keep that element in the set.

So at the end, we're left with only those elements that are duplicates, and we can then do a remove()(docs).


Original:

This will take care of your duplicates for you.

$('div>div').remove()



On a site I was working on the "seen" jQuery solution wasn't working. What I used was this:

HTML

<div id="t" class="resevt">2</div>
<div id="t" class="resevt">tues</div>
<div id="w" class="resevt">wed</div>
<div id="th" class="resevt">4</div>
<div id="th" class="resevt">thurs</div>
<div id="f" class="resevt">fri</div>
<div id="count"></div>


jQuery

var ids = [];
$('*').each(function() {
    if (this.id && this.id !== '') {
        if (ids[this.id]) {
            $(this).remove();
        } else {
            ids[this.id] = this
        }
    }
});


I used this:

            // REMOVING SECOND DUPLICATE SECTIONS OF DIVS HERE 
            \$("div[id^='EXTRACTION_']").each(function(){
               var ids = \$('[id='+this.id+']');
               if(ids.length>1 && ids[0]==this)
               {  
                     \$(ids[1]).remove();
               }
            });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜