3

I know it's possible to use Jquery to search for a duplicated <li> tag and remove it, but what I accually want to do is when a duplicate is found, remove both instances of the li.

Example:

<li>foo
<li>bar
<li>foo
<li>test
<li>me
<li>bar

the Jquery should return the output

<li>test
<li>me

Any Ideas?

Thanks in advance.

4

1 に答える 1

3

最初に頭に浮かんだのは:

$("li").filter(function() {
    var text = this.innerHTML,
        el = $(this).siblings("li").filter(function() {
            return this.innerHTML == text;
        }).remove();
    return el.length > 0;
}).remove();​

デモ: http://jsfiddle.net/x5QQZ/

于 2012-09-05T11:26:18.853 に答える