开发者

Hide All But First Matching Element

I am using jquery to sort through multiple paragraphs. Currently I have it set to show only paragrap开发者_开发问答hs that start with a current letter. But now, I would like to consolidate further. If the text between the paragraph tags has multiple instances, I would like all but the first hidden.

This is what I have so far but, it is not working.

var letter = '<?php  echo(strlen($_GET['letter']) == 1) ? $_GET['letter'] : ''; ?>'

function finish(){
    jQuery('p').each(function(){     
    if(jQuery(this).text().substr(0,1).toUpperCase() == letter){
         jQuery(this).addClass('current-series');
         jQuery(this).html(letter + '<span class="hidden">'+jQuery(this).text().slice(1)+ '</span>');
    }
    else{ jQuery(this).hide();}
   })
}

Update:

Sorry guys, I know this is kind of hard to explain. Here's a basic example:

  • The selected letter is B
  • The values returned are:

Ball

Ball

Ball

Boy

Brain

Bat

Bat

Each of these values is in a paragraph tag. Is there a way to consolidate to this?

Ball

Boy

Brain

Bat


OK, I think I understand now.

Try this: http://jsfiddle.net/Upa3S/

    var letter = 'B';

    var array = [];  // Stores array of the content of paragraphs

    function finish(){
        jQuery('p').each(function(){

               // Cache reference to the paragraph
            $th = jQuery(this);
            if($th.text().substr(0,1).toUpperCase() == letter) {

                    // Test if we've already stored the current content in array
                if(jQuery.inArray($th.text(), array) > -1) {
                       // If so, hide it
                    $th.addClass('hidden'); // You can do $th.hide() if you prefer
                } else {
                       // If not, put its content in the array
                     $th.addClass('current-series');
                    array.push($th.text());
                }
            } else {
                $th.hide();
            }
       })
    }

finish();​

Or using .filter()

http://jsfiddle.net/Fjj5d/

var letter = 'B';

var array = [];

function finish(){
    jQuery('p').filter(function(){
        $th = jQuery(this);
        if($th.text().substr(0,1).toUpperCase() == letter && jQuery.inArray($th.text(), array) < 0 ) {
            array.push($th.text());
            $th.addClass('current-series');
        } else {
            return this;
        }
    }).hide();
}
finish();


This will hide all but the first paragraph with that first letter:

EDIT: You can do the following messy solution.

function finish(){
    var found_first = [];
    jQuery('p').each(function(){     
    if(jQuery(this).text().substr(0,1).toUpperCase() == letter){
    if(found_first[jQuery(this).text()] != true){
        jQuery(this).addClass('current-series');
        found_first[jQuery(this).text()] = true;
    }else{
        jQuery(this).hide();
    }
    }
    else{ jQuery(this).hide();}
   })

}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜