Jquery 隐藏除第一个匹配元素外的所有元素
我使用jquery对多个段落进行排序。目前,我将其设置为仅显示以当前字母开头的段落。但现在,我想进一步巩固。如果段落标记之间的文本有多个实例,我希望隐藏除第一个以外的所有实例 这是我到目前为止所做的,但它不起作用Jquery 隐藏除第一个匹配元素外的所有元素,jquery,html,Jquery,Html,我使用jquery对多个段落进行排序。目前,我将其设置为仅显示以当前字母开头的段落。但现在,我想进一步巩固。如果段落标记之间的文本有多个实例,我希望隐藏除第一个以外的所有实例 这是我到目前为止所做的,但它不起作用 var letter = '<?php echo(strlen($_GET['letter']) == 1) ? $_GET['letter'] : ''; ?>' function finish(){ jQuery('p').each(function(){
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();}
})
}
var字母=“”
函数finish(){
jQuery('p')。每个(函数(){
if(jQuery(this).text().substr(0,1).toUpperCase()==字母){
jQuery(this).addClass('current-series');
jQuery(this).html(字母+''+jQuery(this).text().slice(1)+'');
}
else{jQuery(this.hide();}
})
}
更新:
对不起,伙计们,我知道这很难解释。以下是一个基本示例:
- 所选字母为B
- 返回的值为:
Bat这将隐藏除第一段外的所有带有第一个字母的段落: 编辑:您可以执行以下混乱的解决方案
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();}
})
}
好的,我想我现在明白了 试试这个:
或者使用
.filter()
var字母='B';
var数组=[];
函数finish(){
jQuery('p')。过滤器(函数(){
$th=jQuery(this);
if($th.text().substr(0,1).toUpperCase()==letter&&jQuery.inArray($th.text(),array)<0){
array.push($th.text());
$th.addClass(“当前系列”);
}否则{
归还这个;
}
}).hide();
}
完成();
除了第一段还是第一个字母?我以为你指的是实际的第一个字母。啊,当你最初获得数据时,最好是在PHP/Mysql方面这样做,否则会变得混乱。我想我理解你现在在寻找什么。我更新了我的答案,jsFiddle.com也再次更新了我的答案。我的仍然隐藏不以指定字母开头的段落。不过我还是要说做这个服务器端。亚伦-你又做了一次。非常感谢你。希望有一天我能像你一样熟练地使用Jquery:如果你用MySQL选择数据,你可以选择这样的数据来获得所有以指定字母开头的数据,并且只有唯一的名称:
select title FROM table WHERE title like'a%'GROUP BY title代码>是的,我已经考虑过了,但不幸的是,mysql在应用代码的地方将不可用。谢谢:)在发送到浏览器之前用PHP做同样的事情怎么样?如果你有一个数组:
。它删除了重复的“绿色”和“红色”。@Batfan-不客气。如果您感兴趣,我使用.filter()
找到了一种更简洁的方法。免费;o)
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();
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();