隐藏所有,但第一个匹配元素 [英] Hide All But First Matching Element

查看:71
本文介绍了隐藏所有,但第一个匹配元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jquery对多个段落进行排序。目前,我已将它设置为仅显示以当前字母开头的段落。但现在,我想进一步巩固。如果段落标签之间的文本有多个实例,我想除了第一个隐藏以外的所有文本。



这是我目前为止的,但它不起作用。 / p>

  var letter ='<?php echo(strlen($ _ GET ['letter'])== 1)? $ _GET ['letter']:''; (){
if(jQuery(this).text()。substr(')'

函数finish(){
jQuery (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() ;}
})
}

更新:



对不起,我知道这很难解释。这是一个基本的例子:


  • 所选字母是B

  • 返回的值是: li>






小球

男孩

大脑

蝙蝠



蝙蝠



这些值中的每一个都位于段落标记中。
是否有巩固的方法?


Ball



男孩



Brain


解决方案

<

编辑:你可以做下面的混乱的解决方案。


$ b这将隐藏除第一个字母的第一段外的所有内容:

$ b

  function finish(){
var found_first = [];
jQuery('p')。each(function(){
if(jQuery(this).text()。substr(0,1).toUpperCase()== letter){$ b $ (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(); }
})

}


I am using jquery to sort through multiple paragraphs. Currently I have it set to show only paragraphs 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

解决方案

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();}
   })

}

这篇关于隐藏所有,但第一个匹配元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆