2009-07-30 34 views
2

(A jQuery的有關問題在計算器上過濾的東西。我發誓,它不屬於在元!)過濾問題計數

問題
我期待使用的Greasemonkey腳本過濾Home,Questions和Unanswered Page上的問題。現在我有一個解決方案(下面),但需要幾秒鐘來處理,我想知道是否有更好的方法。

背景
如果您還沒有采取一看主頁的結構,這是非常類似:
(簡化問題的目的)

<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)>號)` – 2009-07-30 15:35:51

+0

會做,謝謝。 – 2009-07-30 15:42:41

回答

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

您可以重複使用$('.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

由於某種原因,真的放慢了下來..奇怪。 – 2009-07-30 15:49:04