Jquery按字母排序

时间:2010-06-15 22:37:14

标签: php jquery html

我正在使用jquery对一组段落标记进行排序(对Aaron Harun的称赞)。它从url字符串中提取值“letter”(字母),并仅显示以该字母开头的段落。它会隐藏所有其他内容并整合列表,以便不显示重复项。

参见代码:

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

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


}

整天都在使用这个,我有两个问题:

  1. 有没有办法让它忽略“The”这个词,如果它是第一个?例如,如果一个段落以“The Amazing”开头,我希望它显示在“A”页面上,而不是“T”页面,就像它现在一样。

  2. 有没有办法为(所有)数字设置单页?例如,页面的URL将类似于domain.com/index.php?letter=0,这将仅显示以数字,任何数字开头的段落标记。我现在可以使用单个数字执行此操作,但是,我希望所有数字都有1页。

1 个答案:

答案 0 :(得分:2)

你好!我最终重写了脚本,但为你做了demo:)

/* Return a unique array */
Array.prototype.getUnique = function(){
 var u = {}, a = [], i, l;
 for(i = 0, l = this.length; i < l; ++i){
  if(this[i] in u) continue;
  a.push(this[i]);
  u[this[i]] = 1;
 }
 return a;
}

/* Show paragraph containing first letter (or number if zero) */
function finish(letter){
 var t, txt, found = [];
 if (letter.match(/\d/)) { letter = "0"; } // set letter to zero if it is any number
 $('p').each(function(){
  // grab first 20 characters so we don't need to split the entire paragraph
  txt = $(this).text().substr(0,20).split(' ');
   if (txt[0].toLowerCase() == 'the') { txt.shift(); } // remove first element if it's "the"
  t = txt[0].match(/\d/) ? "0" : txt[0].substr(0,1).toLowerCase(); // set zero for digits or get first letter
  found.push(t); // Add letter/number to array
  if ((t == "0" && letter == "0") || t == letter.substr(0,1).toLowerCase()){ // show paragraph
   $(this).addClass('current-series').show();
  } else {
   $(this).hide();
  }
 })
 return found.getUnique().sort();
}

更新:好的,我终于明白了你的意思是合并了!好极了!你的意思是你只想要显示独特的条目!我updated the demo并且我还添加了一个小脚本来抓取URL中的字母变量,所以你真的不需要使用php来实现这个目的:

在演示中

注意,我不得不删除选择列表,但您可以通过修改此行来更改所选字母(仅限演示):

 var letter = getletter() || '0';

'0'更改为您要显示的任何字母(仅限演示)。在实时版本中,它会自动从URL中获取该字母。

/* get letter from URL */
function getletter(){
 var p = (new RegExp("[\\?&]letter=([^&#]*)")).exec(window.location.href);
 return (p===null) ? "" : p[1];
}

/* Show paragraph containing first letter (or number if zero) */
function finish(){
 var t, txt, found = [];
 // get letter from url or set to zero if it doesn't exist
 var letter = getletter() || '0';
 // set letter to zero if it is any number
 if (letter.match(/\d/)) { letter = "0"; }
 $('p').each(function(){
  // grab first 20 characters so we don't need to split the entire paragraph
  txt = $(this).text().substr(0,20).split(' ');
  // remove first element if it's "the"
  if (txt[0].toLowerCase() == 'the') { txt.shift(); }
  t = txt[0].match(/\d/) ? "0" : txt[0].substr(0,1).toLowerCase(); // set zero for digits or get first letter
  // show paragraph if unique and matches letter
  if ((found[$(this).text()] != true) && ((t == "0" && letter == "0") || t == letter.substr(0,1).toLowerCase())){
   $(this).addClass('current-series').show();
   found[$(this).text()] = true;
  } else {
   $(this).hide();
  }
 })
}