2009-07-30 4 views
2

カウント(私は誓う。StackOverflowの上のフィルタリングのものについてjQueryの質問を、それがメタに属していない!)私はGreasemonkeyのスクリプトを使用しているよフィルタリング質問

質問
ホームページ、質問、未回答ページの質問をフィルタリングします。今、私はそれのための解決策を持っていますが、処理に数秒かかるので、より良い方法があるかどうか疑問に思っていました。 (質問のために簡略化)

<div class="question-summary"> 
    <div class="status"> 
     <div class="mini-counts">1</div> 
     <div>answers</div> 
    </div> 
</div> 

、これはです:あなたは、ホームページの構造を見て取られていない場合は

背景
が、それはこの非常によく似ています質問/未回答ページの構造:

<div class="question-summary"> 
    <div class="status"> 
     <strong>1</strong> 
     answers 
    </div> 
</div> 

は今、私は、各質問のうち、「1」(回答数)を取得する必要があり、そしてCそれが特定の数を超えていれば、現在、私はこのコードを使用しています:

function filterByAnswer(number) 
{ 
    $.each($('.question-summary'), function() 
    { 
      // 
    if($('.status .mini-counts',this)) 
    { 
     var answers = $('.status .mini-counts',this).text(); 
    } 
    else if($('.status strong',this)) 
    { 
     var answers = $('.status strong',this).text(); 
    } 
    if(answers > number) 
    { 
     $(this).hide(); 
    }  
}); 
} 

、私の質問は、これを行うためのより高速な方法はありますか?私はこれを処理するのに数秒かかることがわかりました。

+0

:あなたは(答え>番号)場合は '変更する必要があります'おかげで、(のparseInt(回答、10)>番号) ' –

+0

を行うかどう'へ。 –

答えて

1

これを試してください。質問とは無関係の

function filterByAnswer(number) 
{ 
    $('.question-summary').filter( 
     function() { 
      var answers = $(this).find('.status').children('.mini-counts, strong').text(); 
      return parseInt(answers, 10) < number; 
     }).hide();  
}  
+0

はい!それはまさに私が必要なものです! –

0

$('.status .mini-counts',this)$('.status strong',this)の値を再利用できます。

var $miniCounts = $('.status .mini-counts',this); 

if($miniCounts) 
    { 
      var answers = $miniCounts.text(); 
    } 
    else 
    { 
     var $strong = $('.status strong',this); 
     if($strong) { 
      var answers = $strong.text(); 
     } 
    } 
+0

何らかの理由でそれが本当に遅くなりました。奇妙です。 –